Call a swift function from java script that returns a value using wk webview

五迷三道 提交于 2021-02-08 07:42:35

问题


I want to call a swift function from java script code which returns the device id back to the script, I had added the code that use currently. Please advise how to return the value back to the java script from the swift function, Thanks in advance

Code Snippet :

     super.viewDidLoad()
    {
    self.webView = WKWebView()
    let preferences = WKPreferences()
    preferences.javaScriptEnabled = true
    let configuration = WKWebViewConfiguration()
    configuration.preferences = preferences
    configuration.userContentController = contentController
    configuration.userContentController.addScriptMessageHandler(self,name: "interOp")
    self.webView = WKWebView(frame: self.view.frame, configuration: configuration)
    print(self.view.frame)
    self.view = self.webView
    webView!.navigationDelegate = self
    let url = NSURL(string:"http://softence.com/devTest/vb_ios.html")
    let req = NSURLRequest(URL:url!)
    self.webView!.loadRequest(req)

    }

// did receivescript message is working fine
func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage)
{
    let sentData = message.body as! NSDictionary
    print(sentData)
    if sentData["name"] as! String == "DeviceInfo"
    {
        self.DeviceInfo()
    }
}
// i want to return the following device info to javascript
func DeviceInfo() -> String
{
let dict = NSMutableDictionary()
    dict.setValue(UIDevice.currentDevice().model, forKey: "model")
    dict.setValue(UIDevice.currentDevice().name, forKey: "name")
    dict.setValue(UIDevice.currentDevice().systemVersion, forKey: "system_version")
    return String(dict)
}

回答1:


Try having a look at the evaluateJavaScript(_:, completionHandler:) function on WKWebView as described here

To use that, your DeviceInfo function should define the complete JavaScript string which you would like to execute.

So for instance if you had a JavaScript function defined like so:

showDeviceInfo(model, name, system_version) {

}

Then your DeviceInfo function could look something like this:

func deviceInfo() {
    let model = UIDevice.currentDevice().model
    let name = UIDevice.currentDevice().name
    let systemVersion = UIDevice.currentDevice().systemVersion

    let javaScriptString = "showDeviceInfo(\(model), \(name), \(systemVersion));" //string your JavaScript string together from the previous info...and no...it aint pretty :)
    webView.evaluateJavaScript(javaScriptString, completionHandler: nil)
}

You could also return javaScriptString from your DeviceInfo function and then call it in your userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage), the important things are:

  1. you need to define the entire JavaScript string you would like to execute
  2. you need to call evaluateJavaScript

Hope that helps you.



来源:https://stackoverflow.com/questions/39632957/call-a-swift-function-from-java-script-that-returns-a-value-using-wk-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!