I’ve a great problem. I must pass a variable of a javascript function into an objective-C variable.
These are my code’s parts:
Javascript:
function abc(){
something...
}
Objective-C:
myLabel.text = [webView stringByEvaluatingJavaScriptFromString:@"documentElement.textContent"];
The javascript code prints to video a number that I want use in my Objective-C code but with this nothing appears, instead using this code:
myLabel.text = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];
It pass the value that I want and the entire code of the javascript function!
Please some advice…. Thanks at all!!!
`
You wan to call the function, not just refer to it:
the key bit is at the end:
Since JavaScript functions are first-class objects, when you do:
…
xreceives a reference to the function. In your case, since the UIWebView then coerces it to a string, you end up callingtoStringon the function, which on most JavaScript implementations gives you the code of the function (although this is not specified by any standard).To actually call a function, you put parentheses after it (with nothing in them, if there are no arguments you need to pass; or with arguments in them if you do), e.g.:
Disclosure: I know nothing about iOS development, Objective-C, or
UIWebView, but I was able to quickly find thestringByEvaluatingJavaScriptFromStringfunction you were using, and then confirmed via this page that the string after the@needs to be valid JavaScript code.