WKWebView 拦截器WKContentRuleListStore

WKContentRuleList是应用于Web内容的编译规则列表,适用于 iOS11.0+ 的设备。我们可以通过给webView.configuration.userContentController添加WKContentRuleList,使WebView的加载遵守相关规则。

记录下
  • 优酷moblie视频播放拦截json
[{"trigger" : {"url-filter" : ".*cp31.ott.cibntv.net\/.*"},"action" : {"type" : "block"}},
{"trigger" : {"url-filter" : ".*pl-ali.youku.com.*"},"action" : {"type" : "block"}}]

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[self configMakeHttps] options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
WKUserContentController *_userContentController = [[WKUserContentController alloc] init];
WKWebViewConfiguration *_configuration = [[WKWebViewConfiguration alloc] init];

if (@available(iOS 11.0, *)) {
  [[WKContentRuleListStore defaultStore] compileContentRuleListForIdentifier: @"ContentBlockingRules" encodedContentRuleList: jsonStr completionHandler:^(WKContentRuleList *contentRuleList, NSError *error) {
  [_configuration.userContentController addContentRuleList:contentRuleList];
  }];
} else {
  // Fallback on earlier versions
}

使用介绍
官方文档

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。