The method you are trying to call is prototyped as follows:
func stringByEvaluatingJavaScriptFromString(_ script: String) -> String?
It means:
String required as a single parameter- It returns an optional
String ( String? )
To use it, there must be an instance of UIWebView :
let result = webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitUserSelect='none'")
Because the return type is optional , you must expand it before you can use it. But be careful, it may not matter (that is, it may be nil ), and the expanding nil values ββwill lead to runtime failures.
So, you need to check this before you can use the returned string:
if let returnedString = result { println("the result is \(returnedString)") }
This means: if result not nil , then expand it and assign it to a new constant called returnedString .
Alternatively, you can wrap it along 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.
source share