deq*_*uin 5 ios ios8 wkwebview
我有一个加载网页的应用程序,但阻止下载图像,字体,javascripts等.为此,我实现了一个NSURLProtocol子类,它与UIWebView非常合作.
但是我正在迁移WKWebview,并意识到我的精心设计的NSURLProtocol类不再能够过滤掉这些资源.
有谁知道如何实现过滤/阻止?
如果您想知道我是如何进行迁移的,我开始阅读这篇文章:http://floatlearning.com/2014/12/uiwebview-wkwebview-and-tying-them-together-using-swift/
deq*_*uin 14
从iOS 11开始,您就可以使用了 WKContentRuleList
首先,创建内容规则或列表.每个规则都包含一个触发器和一个动作.请参阅 Apple关于内容规则创建的文档
这是一个创建示例,阻止所有图像和样式表内容,但允许那些以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"
}
}]
"""
Run Code Online (Sandbox Code Playgroud)
拥有规则列表,您可以将它们添加到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)
}
Run Code Online (Sandbox Code Playgroud)
如果以后要删除所有规则,请致电:
self.wkWebView.configuration.userContentController.removeAllContentRuleLists()
self.wkWebView.load(self.request)
Run Code Online (Sandbox Code Playgroud)
祝你好运!
我在Github WKContentRuleExample上创建了一个示例项目
如果其他人对仅离线WKWebView感兴趣:以下方法是对 @dequin 答案的修改。它使用内容阻止规则来阻止对远程资源(不以 开头的 URL file://)的所有请求:
import Cocoa
import WebKit
// Block all URLs except those starting with "file://"
let blockRules = """
[
{
"trigger": {
"url-filter": ".*"
},
"action": {
"type": "block"
}
},
{
"trigger": {
"url-filter": "file://.*"
},
"action": {
"type": "ignore-previous-rules"
}
}
]
"""
Run Code Online (Sandbox Code Playgroud)
/// `WKWebView` which only allows the loading of local resources
class OfflineWebView: WKWebView {
override init(frame: CGRect, configuration: WKWebViewConfiguration) {
WKContentRuleListStore.default().compileContentRuleList(
forIdentifier: "ContentBlockingRules",
encodedContentRuleList: blockRules
) { contentRuleList, error in
if let error = error {
// Handle error
} else if let contentRuleList = contentRuleList {
configuration.userContentController.add(contentRuleList)
} else {
// Handle error
}
}
super.init(frame: frame, configuration: configuration)
}
}
Run Code Online (Sandbox Code Playgroud)