Home > DeveloperSection > Forums > How to get javascript variable's value in iOS app?
ezra heywood
ezra heywood

Total Post:145

Posted on    November-12-2014 3:37 AM

 iPhone JavaScript  Ios  Objective C 

 1 Reply(s)
 807  View(s)
Rate this:

I need to access a javascript variable which is declared inside a try block in the script

var ErrorResponse = "";

in my iOS app. I don't have much idea about javascript thats why I am unable to find out the way to do it. I am using this:

NSString *jsString = @"var temp = ErrorResponse; return temp";

NSString *myFieldValue1 = [self.viewYTParser stringByEvaluatingJavaScriptFromString:jsString];

to retrieve the value but it returns an empty string.

Elena Glibart
Elena Glibart

Total Post:42

Posted on    November-12-2014 6:46 AM

Here is the code that should do the magic:

NSString *errorResponse = [self.viewYTParser stringByEvaluatingJavaScriptFromString:@"ErrorResponse"];

Just execute this line each time you need to retrieve the up-to-date value of ErrorResponse.

You shouldn't use the return keyword.
If you want your JS to return any value from stringByEvaluatingJavaScriptFromString then it should be something that you would regularly write after the return keyword in regular JS function - without the return word itself.

Don't want to miss updates? Please click the below button!

Follow MindStick