ios - How to call JavaScript function with/without parameter in WKWebView? -


i have loaded website url in wkwebview. @ stage after handling wkscriptmessagehandler, did native operation.

after finish native operation want call java script function in existing loaded wkwebview.

for testing javascript function simple

function try_once() {     // code } 

and in swift did:

let str = "try_once();"  self.wkweb.evaluatejavascript(str, completionhandler: { (obj, error) in                  if error != nil                 {                     print("error: \(string(describing: error))")                 }                 else                 {                     print("object : \(string(describing: obj))")                 }             }) 

but got error:

error domain=wkerrordomain code=4 "a javascript exception occurred" userinfo={wkjavascriptexceptionlinenumber=1, wkjavascriptexceptionmessage=referenceerror: can't find variable: try_once, wkjavascriptexceptionsourceurl=http://someurl/user/signin/?_next=/v2/#, nslocalizeddescription=a javascript exception occurred, wkjavascriptexceptioncolumnnumber=9})

it might because javascript function trying execute not loaded @ time of method call. can add observer execute after loading of script. adding observer @ answer.

reference: can't find variable error when calling method

thanks. hope helps.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -