简体   繁体   中英

How use stringByEvaluatingJavaScript with params

I'am trying to do this function in Swift 3

self.dict = result as! [String : AnyObject]
self.webView.stringByEvaluatingJavaScript(from: "setFacebookResult(\(self.dict),\(self.userToken))")

I have to pass the token and the result to a global function in javascript but this params dont go correctly to the method. Pls help me!

You have to format it correctly for javascript.

Note that when using strings, you can't do something like:

let aString = "myTestString"
self.webView.stringByEvaluatingJavaScript(from: "var myString = \(aString)")

instead you have to format it with "" :

let aString = "myTestString"
self.webView.stringByEvaluatingJavaScript(from: "var myString = \"\(aString)\"")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM