从使用 wk webview 返回值的 java 脚本调用 swift 函数 [英] Call a swift function from java script that returns a value using wk webview

查看:44
本文介绍了从使用 wk webview 返回值的 java 脚本调用 swift 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 java 脚本代码调用一个 swift 函数,该函数将 device id 返回给脚本,我已经添加了当前使用的代码.请告知如何将值从 swift 函数返回到 java 脚本,提前致谢

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

代码片段:

     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)
}

推荐答案

尝试查看 WKWebView 上的 evaluateJavaScript(_:, completionHandler:) 函数,如下所述此处

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

要使用它,您的 DeviceInfo 函数应该定义您想要执行的完整 JavaScript 字符串.

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

例如,如果你有一个像这样定义的 JavaScript 函数:

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

showDeviceInfo(model, name, system_version) {

}

那么您的 DeviceInfo 函数可能如下所示:

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)
}

您也可以从 DeviceInfo 函数返回 javaScriptString,然后在 userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) 中调用它,重要的是:

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. 您需要定义要执行的整个 JavaScript 字符串
  2. 你需要调用evaluateJavaScript

希望能帮到你.

这篇关于从使用 wk webview 返回值的 java 脚本调用 swift 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆