【问题标题】:Block ads in WKWebView在 WKWebView 中屏蔽广告
【发布时间】:2020-07-28 21:03:49
【问题描述】:

我想在我的 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】:

    试试这个:

    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 你正在请求特定的域

    【讨论】:

      【解决方案2】:

      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

      【讨论】:

      • WKJson 链接不起作用。你能帮帮我吗
      猜你喜欢
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多