javascript Swift stringByEvaluatingJavaScriptFromString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26056132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift stringByEvaluatingJavaScriptFromString
提问by Fabian Boulegue
I try to use some javascript on my WebView with the new
我尝试在我的 WebView 上使用一些 javascript 和新的
stringByEvaluatingJavaScriptFromString
function
stringByEvaluatingJavaScriptFromString
功能
I m not quiet familiar with the syntax so i tried
我不太熟悉语法,所以我尝试了
func stringByEvaluatingJavaScriptFromString( "document.documentElement.style.webkitUserSelect='none'": String) -> String?
as shown here https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebView_Class/index.html#//apple_ref/occ/instm/UIWebView/stringByEvaluatingJavaScriptFromString: but i get a error that "Expected '.' separator"
如此处所示https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebView_Class/index.html#//apple_ref/occ/instm/UIWebView/stringByEvaluatingJavaScriptFromString:但我收到一个错误“预期” .' 分隔器”
回答by Nicolas B.
The method you are trying to call is prototyped as the following:
您尝试调用的方法原型如下:
func stringByEvaluatingJavaScriptFromString(_ script: String) -> String?
func stringByEvaluatingJavaScriptFromString(_ script: String) -> String?
This means :
这意味着 :
- It takes a
String
as single parameter - It returns an optional
String
(String?
)
- 它需要一个
String
作为单个参数 - 它返回一个可选的
String
(String?
)
You need to have an instance of UIWebView
to use it:
您需要有一个实例UIWebView
才能使用它:
let result = webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitUserSelect='none'")
Because the return type is optional, it needs to be unwrapped before you can use it.
But be careful, it may not have a value (i.e. it may be equal to nil
) and unwrapping nil values leads to runtime crashes.
因为返回类型是optional,所以在使用之前需要解包。但要小心,它可能没有值(即它可能等于nil
)并且解包 nil 值会导致运行时崩溃。
So you need to check for that before you can use the returned string:
因此,您需要先检查一下,然后才能使用返回的字符串:
if let returnedString = result {
println("the result is \(returnedString)")
}
This means: If result
is not nil
then unwrap it and assign it to a new constant called returnedString
.
这意味着:如果result
不是,nil
则将其解包并将其分配给名为 的新常量returnedString
。
Additionally, you can wrap it together with:
此外,您可以将其包装在一起:
let script = "document.documentElement.style.webkitUserSelect='none'"
if let returnedString = webView.stringByEvaluatingJavaScriptFromString(script) {
println("the result is \(returnedString)")
}
Hope this makes sense to you.
希望这对你有意义。
回答by Bharathi Devarasu
This method is used to call the javascript script directly from uiwebview
该方法用于直接从uiwebview调用javascript脚本
let htmlTitle = myWebView.stringByEvaluatingJavaScriptFromString("document.title");
println(htmlTitle)
http://sourcefreeze.com/uiwebview-example-using-swift-in-ios/
http://sourcefreeze.com/uiwebview-example-using-swift-in-ios/
回答by maxwell
1) Create JavaScript constant:
1) 创建 JavaScript 常量:
let jsString: String = "document.documentElement.style.webkitUserSelect='none'"
2) Using evaluateJavaScriptfunction for WKWebView(iOS 8.0+)
2)为WKWebView(iOS 8.0+)使用evaluateJavaScript函数
self?.webView?.evaluateJavaScript("", completionHandler: { result, error in
print(result)
})
or stringByEvaluatingJavaScriptFromStringfor UIWebView(iOS 2.0–12.0)
或stringByEvaluatingJavaScriptFromString用于UIWebView(iOS 2.0–12.0)
let result = webView.stringByEvaluatingJavaScriptFromString(jsString)
print(result)
Also from Apple doc:
同样来自 Apple 文档:
The stringByEvaluatingJavaScriptFromString: method waits synchronously for JavaScript evaluation to complete. If you load web content whose JavaScript code you have not vetted, invoking this method could hang your app. Best practice is to adopt the WKWebView class and use its evaluateJavaScript:completionHandler: method instead.
stringByEvaluatingJavaScriptFromString: 方法同步等待 JavaScript 评估完成。如果您加载的 Web 内容的 JavaScript 代码未经您,则调用此方法可能会挂起您的应用程序。最佳实践是采用 WKWebView 类并改用它的evaluateJavaScript:completionHandler: 方法。