在WKWebView中屏蔽广告

问题描述 投票:2回答:1

我想在我的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"
      }
    }]
    """
ios objective-c wkwebview adblock
1个回答
0
投票

尝试一下:

"if-domain":["googleads.g.doubleclick.net*","pagead.googlesyndication.com*","pagead1.googlesyndication.com","pagead2.googlesyndication.com*"]

如果添加此:googleads.g.doubleclick.net您正在请求特定域

© www.soinside.com 2019 - 2024. All rights reserved.