由于iOS的11你可以使用WKContentRuleList
首先,创建内容规则或列表。每条规则都由一个触发器和一个动作组成。见 Apple's Documentation on Content Rule creation
这是一个创建实例,阻止所有图像和样式表的内容,但允许通过忽略以前的规则的方式对JPEG的结束:
let blockRules = """
[{
"trigger": {
"url-filter": ".*",
"resource-type": ["image"]
},
"action": {
"type": "block"
}
},
{
"trigger": {
"url-filter": ".*",
"resource-type": ["style-sheet"]
},
"action": {
"type": "block"
}
},
{
"trigger": {
"url-filter": ".*.jpeg"
},
"action": {
"type": "ignore-previous-rules"
}
}]
"""
有规则清单,你可以将它们添加到ContentRuleListStore
import WebKit
@IBOutlet weak var wkWebView: WKWebView!
let request = URLRequest(url: URL(string: "https://yourSite.com/")!)
WKContentRuleListStore.default().compileContentRuleList(
forIdentifier: "ContentBlockingRules",
encodedContentRuleList: blockRules) { (contentRuleList, error) in
if let error = error {
return
}
let configuration = self.webView.configuration
configuration.userContentController.add(contentRuleList!)
self.wkWwebView.load(self.request)
}
如果以后要删除所有的规则,请致电:
self.wkWebView.configuration.userContentController.removeAllContentRuleLists()
self.wkWebView.load(self.request)
这里是2017 WWDC video
最佳卢克斯的!
我创建了一个示例项目on Github WKContentRuleExample
我会说这不是WebKit的错误。 Android已经有相当长的一段时间了。阻止图像加载(以及其他许多事情)从一开始就一直存在。苹果公司根本没有公开这些功能。 – Russ
提交错误是让苹果知道这是我们可以访问的功能的最佳方式。 –