vinny
vinny

Reputation: 529

Hide specific DIV on webpage in WKWebview

I am building an iOS app and using WKWebview to link to a external website (we don't control this website) in WKWebview.

It all works fine but there are certain parts of the webpage, I do not want to show in the iOS app.

After doing some research on the webpage code, I found the DIV which contains the data we want to hide but how do I hide it in WKWebview ?

Upvotes: 3

Views: 2532

Answers (1)

dattk
dattk

Reputation: 146

    let contentController = WKUserContentController()
    let script = "var el = document.getElementById('YourDivID'); if (el) el.parentNode.removeChild(el);"
    let scriptInjection = WKUserScript(source: script as String, injectionTime: WKUserScriptInjectionTime.AtDocumentEnd, forMainFrameOnly: false)
    contentController.addUserScript(scriptInjection)
    let config = WKWebViewConfiguration()
    config.userContentController = contentController
    wkwebView = WKWebView(frame: CGRectZero, configuration: config)

Upvotes: 5

Related Questions