Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
146
rated 0 times [  151] [ 5]  / answers: 1 / hits: 16568  / 10 Years ago, fri, september 26, 2014, 12:00:00

I try to use some javascript on my WebView with the new



stringByEvaluatingJavaScriptFromString function



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


More From » ios

 Answers
9

The method you are trying to call is prototyped as the following:



func stringByEvaluatingJavaScriptFromString(_ script: String) -> String?



This means :




  • It takes a String as single parameter

  • It returns an optional String (String?)



You need to have an instance of UIWebView to use it:



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.



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.



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.


[#69329] Wednesday, September 24, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cullenmaxl

Total Points: 414
Total Questions: 112
Total Answers: 87

Location: United States Minor Outlying Island
Member since Sat, May 28, 2022
2 Years ago
;