javascript - 如何将值从 javascript 传递到 iOS UIWebView
问题描述
我需要将 javascript 变量传递给 iOS UIWebView,我很容易用 Android 做同样的事情,但不能在这里做。
我需要的是我将单击 webview 中的一个链接,该链接将反过来buttonClickPAss
用一些参数调用这个 JS 函数,我想要我在 web 端传递给本地 swift 代码的所有参数。不仅仅是调用那个 JS 函数并迅速返回它的值。
我将在下面添加整个 html 代码
<html>
<head>
<script type="text/javascript">
function buttonClickPAss(a,b,c)
{
// console.log(action);
return a;// return what i passed to the function that is valueA
}
</script>
</head>
<body>
<b><a href='#' onclick="buttonClickPAss('valueA','valueB','valueC');"> Click here from webview</a></b>
</body>
</html>
加载 html 页面时填充此“valueA”、“valueB”和“valueC”我在我的 swift 代码中需要这些值
然后,我将在 webview 中加载上面的 html 页面,然后单击 webview 中的链接,以便调用 JS 函数,因为我的值正在网页中填充。
这是我在android中尝试过的教程
但是当我在 ios 中搜索相同的内容时,我发现了本教程
<script type="text/javascript">
function buttonClickPAss()
{
// console.log(action);
return 'hi from js';
}
</script>
以上是我的js代码
在本机 swift 中,我在加载 url 后给出了这段代码
func webView(_ webView: UIWebView, shouldStartLoadWithRequest request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let url = request.url!
let result = webView.stringByEvaluatingJavaScript(from: "buttonClickPAss()")
print("Result = ",result!)
return true
}
上面的代码工作正常,当我点击触发这个 js 函数的链接时,我的 xcode 中的日志是“hi from js”
但我需要将值传递给函数buttonClickPAss
,所以我将上面的代码更改为此
在 JS 中
<script type="text/javascript">
function buttonClickPAss(a,b,c)
{
// console.log(action);
return a;// return what i passed to the function
}
</script>
在 swift 方面,我将上面的 swift 代码更改为此
func webView(_ webView: UIWebView, shouldStartLoadWithRequest request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let url = request.url!
let result = webView.stringByEvaluatingJavaScript(from: "buttonClickPAss(a,b,c)")
print("Result = ",result!)
return true
}
但在这里“结果”没有打印任何东西,
我也搜索了其他教程,它处理带有空参数的函数,有人可以帮助我吗
还要注意,根据上面的android教程,我可以将三个变量中的js变量分别传递给android,ios swift中是否有类似的机制?最坏的情况,我会将其设为 json 字符串并将其作为单个字符串返回
请帮忙
谢谢
解决方案
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet weak var webView: WKWebView!
var activityIndicator: UIActivityIndicatorView?
func setupWebView(){
webView.navigationDelegate = self
activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
activityIndicator?.center = self.view.center
self.view.addSubview(activityIndicator!)
webView.load(URLRequest(url: URL(string: "YourWebSiteLinkHere")!))
webView.configuration.userContentController.add(self, name: "myInterface")
webView.evaluateJavaScript(s, completionHandler: {(string,error) in
print(error ?? "no error")
})
activityIndicator?.startAnimating()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setupWebView()
}
}
extension ViewController: WKScriptMessageHandler{
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
print("Message received: \(message.name) with body: \(message.body)")
}
}
extension ViewController: WKNavigationDelegate{
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.activityIndicator?.stopAnimating()
self.activityIndicator?.removeFromSuperview()
self.activityIndicator = nil
}
}
在 html 代码中
<script type="text/javascript">
function buttonClickPAss(a,b,c){
//The following line will pass the a to swift
window.webkit.messageHandlers.myInterface.postMessage(a)
}
</script>
在上面的代码中,我注册了“myInterface”。因此,无论何时你想从 JavaScript 向 Swift 提供数据,你都可以轻松地做到这一点。现在在您的示例中,当用户单击链接按钮时;buttonClickPAs 将被调用。现在在您的 buttonClickPAss 方法中,该行将 window.webkit.messageHandlers.myInterface.postMessage(a)
变量数据传递给 Swift infunc userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)
方法。
我认为这应该足以解决您的问题。有关更多信息,请访问此SO
推荐阅读
- python - 即使在无头模式下,是否可以在 Selenium 上使用 `element.click()` 和 Chrome?
- jquery - 如何内爆+添加换行符?
- android - 当图像从上到下下降时,如何减小图像尺寸?
- python - flask-restful和flask中命名空间和蓝图的区别
- python - 如果 b 和 c 是 python 中的数组/数据帧,是否可以使用 `a = b 或 c` 构造?
- android - Android 架构组件 (Android Jetpack) 破坏了 Instant Apps
- javascript - ComponentWillMount ReactJS 上的无限循环
- sonarqube - 如何将 sonarqube 扫描报告发布到多个 SonarQube 实例..?
- wordpress - 如何在拒绝谷歌抓取的同时允许 fess 抓取 wordpress
- java - 自定义 JtextField 属性