如何阻止外部资源加载WKWebView?

时间:2015-08-20 13:44:07

标签: ios ios8 wkwebview

我有一个加载网页的应用程序,但是阻止了图像,字体,javascripts等的下载。为此,我实现了一个NSURLProtocol子类,它与UIWebView非常兼容。

但是,我正在迁移到WKWebview,并意识到我精心设计的NSURLProtocol课程不再能够过滤掉这些资源。

有谁知道如何实现过滤/阻止?

如果您想知道我是如何进行迁移的,我开始撰写这篇文章:http://floatlearning.com/2014/12/uiwebview-wkwebview-and-tying-them-together-using-swift/

3 个答案:

答案 0 :(得分:12)

从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

答案 1 :(得分:3)

从iOS 9.0开始,无法拦截WKWebView的网络请求。您可以通过JavaScript以有限的方式执行此操作。

请提交WebKit错误或Apple错误以请求此功能。我们中的许多人都需要这些钩子。

答案 2 :(得分:1)

如果其他任何人都对仅离线WKWebView 感兴趣:以下方法是对@dequin答案的修改。它使用内容阻止规则来阻止对远程资源(不以file://开头的URL)的所有请求:

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"
        }
    }
]
"""
/// `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)
    }
}