Passing Data to and from an Embedded Uiwebview

Passing Data To and From an Embedded UIWebView

Well the problem is that you tried to evaluate your javascript before the webview had the chance to load the page. First adopt the <UIWebViewDelegate> protocol into your view controller:

@interface ViewController : UIViewController <UIWebViewDelegate>

Then connect the delegate of your webview to your view controller, make the request and finally implement the delegate methods. Here is the one that notifies you when the webview has finished loading:

- (void)viewDidLoad
{
[super viewDidLoad];

[self.graphTotal loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"]isDirectory:NO]]];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *userName = [self.graphTotal stringByEvaluatingJavaScriptFromString:@"testFunction()"];
NSLog(@"Web response: %@",userName);
}

PS: One of the limits you must be aware of when you're evaluating javascript this way is that your script must be executed within 10 seconds. So if for example you've waited more than 10 secs to dismiss the alert you would get an error (failed to return after waiting 10 seconds). Find more about the limitations in the docs.

Best approach to pass data from web view to native app (Windows, iOS & Android)

iOS:

More Details

passing-data-to-and-from-an-embedded-uiwebview

ios-pass-values-to-native-app-from

Sample Image

Android:

webkit-communication-with-native-environment

Loading a webpage through UIWebView with POST parameters

Create POST URLRequest and use it to fill webView

NSURL *url = [NSURL URLWithString: @"http://your_url.com"];
NSString *body = [NSString stringWithFormat: @"arg1=%@&arg2=%@", @"val1",@"val2"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];

Communication with embedded (UI)Webviews in existing iOS and Android Apps

For Android this is simpler to do. Take a look at WebView's addJavascriptInterface method. You can create your own object with methods that can be called directly in the HTML javascript.

iOS requires a bit of trickyness. Best solution for these types of problems is a couple things:

  • For callbacks to iOS you will need to basically make up your own URL scheme like native://somehost.com/somepath When your javascript wants to inform the iOS code use window.location = 'native://somehost.com/somepath';
  • Set the UIWebView delegate to an object that defines webView:shouldStartLoadWithRequest:navigationType: it will look something like this

    if ([request.URL.scheme isEqualToString:@"native"]){
    if([request.URL.host isEqualToString:@"somehost.com"]) {
    //Do the code you need to do here, branch off depending
    //on the path and/or host, you can parse parameters here too
    return NO; //This will keep UIWebView from trying to actually load
    //your made up scheme
    }
    }
    return YES; //If the request isn't one you want to intercept return YES/true
    //so UIWebView will load the page
  • To have your iOS code send information or call functions in your javascript you can use WebView's stringByEvaluatingJavaScriptFromString:. This will return the result of a javascript expression so you can also use it to get some information from the page itself. To call a function use something like this

    [webview stringByEvaluationgJavaScriptFromString:@"myJavaScriptFunction();"]

You can also handle the made up scheme in Android by creating a custom WebViewClient and overriding the shouldOverrideUrlLoading method similarly to the iOS code above except the return calls are backwards, you return true if you handled the URL and the WebView should do nothing more, and false if you want the WebView to handle loading. Be sure to create and assign the custom WebViewClient to the WebView using setWebViewClient. To call javascript functions on the actual WebView do something like webview.loadUrl("javascript:myJavaScriptFunction();");

Calling a native (ios)method from an embedded html page inside my IOS app

Almost the same question but you should connect your scheme request in this method.

Stackoverflow- Call a method by clicking a button in a UIWebView

If you want to go more advanced you can use:

WebViewJavascriptBridge



Related Topics



Leave a reply



Submit