美文网首页
WKWebview无图模式

WKWebview无图模式

作者: Anchoriter | 来源:发表于2019-04-15 20:08 被阅读0次

需求: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];
    }
}

相关文章

网友评论

      本文标题:WKWebview无图模式

      本文链接:https://www.haomeiwen.com/subject/hddhvftx.html