最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - Javascript in WKWebView - evaluateJavaScript vs addUserScript - Stack Overflow

programmeradmin4浏览0评论

I am trying to understand the best way to execute javascript with WKWebview

Could someone please give me the use cases / best practices when using WKWebView.

When to use addUserScript and WKScriptMessageHandler and when to use evaluateJavaScript

    let jscript = "my script"
    let userScript = WKUserScript(source: jscript, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
    let userContentController = WKUserContentController()
    userContentController.addUserScript(userScript)

    let webViewConfiguration = WKWebViewConfiguration()
    webViewConfiguration.userContentController = userContentController
    webView = WKWebView(frame: self.view.bounds, configuration: webViewConfiguration)

vs

let myScript
self.wkWebView.evaluateJavaScript(script) { (result, error) in
    if error != nil {
        print("\(error)")
    }
}

I am trying to understand the best way to execute javascript with WKWebview

Could someone please give me the use cases / best practices when using WKWebView.

When to use addUserScript and WKScriptMessageHandler and when to use evaluateJavaScript

    let jscript = "my script"
    let userScript = WKUserScript(source: jscript, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
    let userContentController = WKUserContentController()
    userContentController.addUserScript(userScript)

    let webViewConfiguration = WKWebViewConfiguration()
    webViewConfiguration.userContentController = userContentController
    webView = WKWebView(frame: self.view.bounds, configuration: webViewConfiguration)

vs

let myScript
self.wkWebView.evaluateJavaScript(script) { (result, error) in
    if error != nil {
        print("\(error)")
    }
}
Share Improve this question asked Jan 5, 2017 at 15:13 Ryan HeitnerRyan Heitner 13.6k6 gold badges82 silver badges130 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

Found a nice explanation

http://jonathanblog2000.blogspot.co.il/2016/11/understanding-ios-wkwebview.html

2.3.2 Inject javascript from native code to js DOM emphasized text WKUserContentController allows add (and remove) a WKUserScript to be injected either when the DOM tree starts to load or finishes to load. On the contrary, evaluateJavaScript allows application to execute a javascript snippet on demand at any time.

In my experience, WKUserScript seems to have more privileges, eg: document.cookie="test=test" works with WKUserScript but not with evaluateJavaScript.

However, WKUserScript requires an additional reload() for the javascript you injected to run. It maybe a bug. Sometimes it plicates the code.

WKScriptMessageHandler is a separate thing. It allows your native app to receive messages from javascript. eg, you can add a handler and receives message that is posted from javascript like: "window.webkit.messageHandlers.notification.postMessage({body: "..."});" Refer: http://nshipster./wkwebkit/

Pretty cool stuff that doesn't exist in UIWebView

发布评论

评论列表(0)

  1. 暂无评论