View Full Version : Access objc variable from javascript?
Danneman101
Feb 23, 2009, 07:47 AM
How can I access an objc variable (say an integer) from the javascript-code of a html-page within a uiwebview?
Is it even possible?
jnic
Feb 23, 2009, 08:43 AM
I've had limited success doing something similar by modifying the DOM using stringByEvaluatingJavaScriptFromString: (https://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/Reference/Reference.html#//apple_ref/occ/instm/UIWebView/stringByEvaluatingJavaScriptFromString:), but it's basically trial and error.
Danneman101
Feb 23, 2009, 09:01 AM
There is very little documentation on it, and no examples to mimic/alter.
Was your solution to call the javascript and pass along the variabel as a parameter?
Pseudocode:
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)myJSFunction(returnParameter)
jnic
Feb 23, 2009, 09:28 AM
Was your solution to call the javascript and pass along the variabel as a parameter?
Yup.
[webview stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:@"document.write(\"%d\");", var]];
etc.
Danneman101
Feb 23, 2009, 10:01 AM
Alright, Im getting somewhere - thanks for your help :)
Finally, just let me get this straight: in the code you substitute the %d with var?
jnic
Feb 23, 2009, 11:18 AM
That's right. In this case, "var" is an integer. See stringWithFormat (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithFormat:) and String Format Specifiers (http://developer.apple.com/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html) for more.
Danneman101
Feb 23, 2009, 02:04 PM
Great - thanks again for the help :) Much appreciated!
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.