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