最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Swift stringByEvaluatingJavaScriptFromString - Stack Overflow

programmeradmin5浏览0评论

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 .html#//apple_ref/occ/instm/UIWebView/stringByEvaluatingJavaScriptFromString: but i get a error that "Expected '.' separator"

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./library/ios/documentation/UIKit/Reference/UIWebView_Class/index.html#//apple_ref/occ/instm/UIWebView/stringByEvaluatingJavaScriptFromString: but i get a error that "Expected '.' separator"

Share Improve this question asked Sep 26, 2014 at 9:19 Fabian BoulegueFabian Boulegue 6,60814 gold badges51 silver badges72 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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.

This method is used to call the javascript script directly from uiwebview

let htmlTitle = myWebView.stringByEvaluatingJavaScriptFromString("document.title");
println(htmlTitle) 

http://sourcefreeze./uiwebview-example-using-swift-in-ios/

1) Create JavaScript constant:

let jsString: String = "document.documentElement.style.webkitUserSelect='none'"

2) Using evaluateJavaScript function for WKWebView (iOS 8.0+)

self?.webView?.evaluateJavaScript("", pletionHandler: { result, error in
       print(result)
})

or stringByEvaluatingJavaScriptFromString for UIWebView (iOS 2.0–12.0)

let result = webView.stringByEvaluatingJavaScriptFromString(jsString)
print(result)

Also from Apple doc:

The stringByEvaluatingJavaScriptFromString: method waits synchronously for JavaScript evaluation to plete. 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:pletionHandler: method instead.

发布评论

评论列表(0)

  1. 暂无评论