13

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?

spentak
  • 4,627
  • 15
  • 62
  • 90

3 Answers3

19

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

Community
  • 1
  • 1
sElanthiraiyan
  • 6,000
  • 1
  • 31
  • 38
  • Can you elaborate further on getting data from the javascript? For instance, if you click a button in the html, it will trigger a javascript function. That function has a return value that I need. How do I get that return value? – spentak Oct 07 '11 at 13:18
  • As I mentioned in the answer navigate to an URL from your JS function- it will fire the delegate method. – sElanthiraiyan Oct 07 '11 at 13:31
4
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script

sounds like exactly the method you need. If, for example, the input and output need to interact with the JavaScript can be done with a single method, you can pass a value into JavaScript and get the results with something like the following code:

NSString* jsCode = [NSString stringWithFormat:@"doSomething('%@')", dataToPass];
NSString* resultFromJavaScript = [webView stringByEvaluatingJavaScriptFromString:jsCode];
Matthew Gillingham
  • 3,429
  • 1
  • 22
  • 26
0

Hi we can send the data from objective c to javascript and can callback from javascript easily

Objective-C --> javaScript

In Objective-C class

NSData *locationCountData = [NSJSONSerialization dataWithJSONObject:locationCount   options:NSJSONWritingPrettyPrinted error:nil];
NSString *locationCountString = [[NSString alloc] initWithData:locationCountData encoding:NSUTF8StringEncoding];

NSData *locationCountData = [NSJSONSerialization dataWithJSONObject:locationCount options:NSJSONWritingPrettyPrinted error:nil];
NSString *locationCountString = [[NSString alloc] initWithData:locationCountData encoding:NSUTF8StringEncoding];

NSString *str = [NSString   stringWithFormat:@"testFunction(%@,%@)",locationCountString, locationsListString];
[self.myWebView stringByEvaluatingJavaScriptFromString: str];

in JavaScript file

<script type="text/javascript">
function testFunction(locationCount,locationList)
{
    alert(locationCount + locationList);
}
</Script>
Chirag Kothiya
  • 955
  • 5
  • 12
  • 28
user3540599
  • 721
  • 9
  • 6