I'm sending a value to javascript and then javascript return a result to me. However, It takes time so that the return value (b) stay null because javascript func needs time. I think I needed to create a thread to wait the result. Below code also doesn't return anything.
func webViewDidFinishLoad(_ webView: UIWebView)
{
var b = ""
//Threat gcdc
DispatchQueue.main.async {
b = webView.stringByEvaluatingJavaScript(from: "getURL('\(self.song_number)')")!
}
print(b)
}
Try to use evaluateJavaScript(_:completionHandler:)
webView.evaluateJavaScript("getURL('\(self.song_number)')") { (result, error) in
if let error = error
{
print("\(error)");
return
}
if let song = result as? String
{
print("\(song)");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.