2020-07-21

native 与JS 交互代码

1、 请求下来接口,然后去reload().self.webview.wvaluateJavaScript(js)
setupRequest(with: page)
let js = "location.reload()"
self.webView.evaluateJavaScript(js) { (result, error) in
Log.d(error)
Log.d(result)
}

2、 let jsUrl = "http://baidu.com"
// let js = "window.location.replace())"
// let param = "test param"
let js = "window.location.replace('" + urlstr + "')"

// let js = "location.reload()"
self.webView.evaluateJavaScript(js) { (result, error) in
Log.d(error)
Log.d(result)
}

但是同一个请求资源的地址的话,还是有问题,IPS还是不会跳转到对应题型,方法一更好;

3、 location.href = "你要跳转的链接"
然后在执行reload 就行 了 就不用你之前写的那个方法了
let urlstr = "http://baidu.com"
let js = "window.location.href = '" + urlstr + "'"
self.webView.evaluateJavaScript(js) { (result, error) in
Log.d(error)
Log.d(result)
if((result) != nil){
self.webView.evaluateJavaScript("location.reload()") {(result, error) in
Log.d(result)
Log.d(error)
}
}
}

4、变更一个url地址,然后去location.reload();
let js = "~ function (){ window.location.href='" + urlstr + "' ;location.reload()}()"
Log.d(js)
self.webView.evaluateJavaScript(js) { (result, error) in
Log.d(error)
Log.d(result)
}
5、变更一个URL地址,然后去location.reload()
let js = " function y(){ window.location.href='" + urlstr + "' ;location.reload()};y()"
Log.d(js)
self.webView.evaluateJavaScript(js) { (result, error) in
Log.d(error)
Log.d(result)
}

你可能感兴趣的:(2020-07-21)