需求:WKWebview要求設(shè)置無圖模式嬉愧,不顯示W(wǎng)KWebview中的所有圖片
1.程序開始運(yùn)行默認(rèn)加載規(guī)則稍算,方便后面使用
#import <WebKit/WebKit.h>
/// 定義一個(gè)全局屬性
@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中設(shè)置ContentRuleList
-(void)changeImageContentRule:(BOOL)isImageRule{
WKContentRuleList *rule = xxxx.imageRuleList;
if (isImageRule) {// 有圖
[self.configuration.userContentController removeContentRuleList:rule];
}else{// 無圖
[self.configuration.userContentController addContentRuleList:rule];
}
}