使用 wk webview 从 java 脚本调用 swift 函数,returns 一个值
Call a swift function from java script that returns a value using wk webview
我想从 java 脚本代码调用 swift 函数,return 将 device id
返回到脚本,我添加了当前使用的代码.请告知如何将 return 值从 swift 函数返回到 java script
,在此先感谢
代码段:
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:)
函数,如 here
所述
要使用它,您的 DeviceInfo
函数应该定义 您要执行的完整 JavaScript 字符串 。
例如,如果您有一个 JavaScript 函数定义如下:
showDeviceInfo(model, name, system_version) {
}
那么您的 DeviceInfo
函数可能如下所示:
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
函数 return javaScriptString
然后在您的 userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage)
中调用它,重要的是:
- 您需要定义要执行的整个 JavaScript 字符串
- 你需要打电话给
evaluateJavaScript
希望对你有所帮助。
我想从 java 脚本代码调用 swift 函数,return 将 device id
返回到脚本,我添加了当前使用的代码.请告知如何将 return 值从 swift 函数返回到 java script
,在此先感谢
代码段:
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:)
函数,如 here
要使用它,您的 DeviceInfo
函数应该定义 您要执行的完整 JavaScript 字符串 。
例如,如果您有一个 JavaScript 函数定义如下:
showDeviceInfo(model, name, system_version) {
}
那么您的 DeviceInfo
函数可能如下所示:
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
函数 return javaScriptString
然后在您的 userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage)
中调用它,重要的是:
- 您需要定义要执行的整个 JavaScript 字符串
- 你需要打电话给
evaluateJavaScript
希望对你有所帮助。