在WKWebView中屏蔽广告

时间:2018-06-21 11:08:11

标签: ios objective-c wkwebview adblock

我想在我的WKWebView中屏蔽广告。 我为规则列表创建了一个JSON文件,其中添加了所有广告域,但没有生效。

我用来屏蔽广告的代码:

let jsonString =
        """
[{
  "trigger": {
    "url-filter": ".*",
    "if-domain":["googleads.g.doubleclick.net","pagead.googlesyndication.com","pagead1.googlesyndication.com","pagead2.googlesyndication.com"]
  },
  "action": {
    "type": "block"
  }
}]
"""


WKContentRuleListStore.default().compileContentRuleList(forIdentifier: ruleId1, encodedContentRuleList: jsonString) { [weak self] (contentRuleList: WKContentRuleList?, error: Error?) in
                if let error = error {
                    self?.printRuleListError(error, text: "compile json string literal")
                    return
                }
                if let list = contentRuleList {
                    self?.webview.configuration.userContentController.add(list)
                    UserDefaults.standard.set(true, forKey: ruleId1)
                    completion?()
                }
            }
        }

如果我使用此规则,则它将阻止webView中的图像:

let jsonString =
            """
    [{
      "trigger": {
        "url-filter": ".*",
        "resource-type": ["image", "style-sheet"]
      },
      "action": {
        "type": "block"
      }
    }]
    """

2 个答案:

答案 0 :(得分:0)

尝试一下:

let contentToBlock = """
                {
                  "trigger": {
                    "url-filter": "googleads.g.doubleclick.net*"

                  },
                  "action": {
                    "type": "block"
                  }
                },
                {
                  "trigger": {
                    "url-filter": "pagead.googlesyndication.com*"

                  },
                  "action": {
                    "type": "block"
                  }
                },
                {
                  "trigger": {
                    "url-filter": "pagead1.googlesyndication.com*"

                  },
                  "action": {
                    "type": "block"
                  }
                },
                {
                  "trigger": {
                    "url-filter": "pagead2.googlesyndication.com*"

                  },
                  "action": {
                    "type": "block"
                  }
                }
            """

如果您添加以下内容:googleads.g.doubleclick.net您正在请求特定域

答案 1 :(得分:0)

GitHub上有一个名为WKJSON的框架。

只需复制此代码

import WKJSON

//...

    let BlockerString = """
                    {
                      "trigger": {
                        "url-filter": "googleads.g.doubleclick.net*"
    
                      },
                      "action": {
                        "type": "block"
                      }
                    },
                    {
                      "trigger": {
                        "url-filter": "pagead.googlesyndication.com*"
    
                      },
                      "action": {
                        "type": "block"
                      }
                    },
                    {
                      "trigger": {
                        "url-filter": "pagead1.googlesyndication.com*"
    
                      },
                      "action": {
                        "type": "block"
                      }
                    },
                    {
                      "trigger": {
                        "url-filter": "pagead2.googlesyndication.com*"
    
                      },
                      "action": {
                        "type": "block"
                      }
                    }
                """
    
    
    webView.JSONString(JSONString: BlockerString, webView: WKWebView)

如果要加载JSON文件,请使用此代码

webView.JSONFile(filePath: "blockerList", webView: WKWebView)
    

我强烈建议您使用此JSON this