WKWebview无图模式

需求:WKWebview要求设置无图模式,不显示WKWebview中的所有图片

1.程序开始运行默认加载规则,方便后面使用

#import <WebKit/WebKit.h>
/// 定义一个全局属性
@property (nonatomic, strong, nullable) WKContentRuleList *imageRuleList API_AVAILABLE(ios(11.0));
if (@available(iOS 11.0, *)) {
            NSString *string = @"[{\"trigger\":{\"url-filter\":\".*\",\"resource-type\":[\"image\"]},\"action\":{\"type\":\"block\"}}]";
            [[WKContentRuleListStore defaultStore] compileContentRuleListForIdentifier:@"imageRuleList" encodedContentRuleList:string completionHandler:^(WKContentRuleList * rule, NSError *error) {
                self.imageRuleList = ruleList;
            }];
}

2.切换有图无图模式
自定义WKWebView中设置ContentRuleList

-(void)changeImageContentRule:(BOOL)isImageRule{
    WKContentRuleList *rule = xxxx.imageRuleList;
    if (isImageRule) {// 有图
        [self.configuration.userContentController removeContentRuleList:rule];
    }else{// 无图
        [self.configuration.userContentController addContentRuleList:rule];
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容