我需要将 javascript 变量传递给 iOS UIWebView,我在 Android 上很容易做到了这一点,但在这里做不到。
我需要的是我将点击webview中的一个链接,该链接将依次调用这个带有一些参数的JS函数buttonClickPAss
,并且我想要我在web端传递的所有参数 native swift 代码。不仅仅是调用 JS 函数并在 swift 中返回它的值。
我将在下面添加整个 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 代码中使用这些值
然后,我将在 Web View 中加载上述 html 页面,并单击 Web View 中的链接,以便调用 JS 函数,因为我的值正在网页中填充。
这是我在android中尝试过的教程
但是当我在 ios 中搜索相同内容时,我发现了这个教程
<script type="text/javascript">
function buttonClickPAss()
{
// console.log(action);
return 'hi from js';
}
</script>
上面是我的js代码
在 native 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 提供数据时,您都可以轻松做到。
现在在您的示例中,当用户单击链接按钮时;将调用buttonClickPAss。现在,在您的buttonClickPAss方法中,window.webkit.messageHandlers.myInterface.postMessage(a)
行将在func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)中将变量数据传递给Swift
方法。
我想这应该足以解决你的问题了。欲了解更多信息,请访问此SO
关于javascript - 如何将值从 javascript 传递到 iOS UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50642609/