Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
198
rated 0 times [  200] [ 2]  / answers: 1 / hits: 20856  / 13 Years ago, fri, october 7, 2011, 12:00:00

I have a webpage. I have a javascript file that does a whole lot of stuff. In the app I have an NSString with some crucial data that needs to be processed in the javascript file. How do I get my string value into a certain place in my html so my javascript can read it?



Also, the javascript will produce certain data when actions are performed (like pressing a button), how do I retrieve the data from the webview/javascript so it can be stored in the app?


More From » iphone

 Answers
5

You can call a javascript method and pass the value by using this



NSString *selectedValue = [webViewInstance stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@getAndReturnRadioValue(%d), questionCounterIntValue]];


But to trigger back from javascript you need to use a workaround method,



Implement the following webViewDelegate method



- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

NSLog(@passed data from web view : %@,[[request URL] query]);

if([[[request URL] query] isEqualToString:@clickedOnLineNo])
{
//Do your action here
}
}


You have to navigate to a fake URL from your javascript method like,



window.location = someLink://yourApp/form_Submitted:param1:param2:param3;


on a button click or your required action.



You could take a look at my answer for this question
Submit a form in UIWebView


[#89747] Wednesday, October 5, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jovanymarshalld

Total Points: 676
Total Questions: 94
Total Answers: 81

Location: Thailand
Member since Thu, Apr 22, 2021
3 Years ago
;