Take the 2-minute tour ×

I have an iPad app that contains a webview. The webview's page has a button on it that, when clicked, needs to somehow tell the native app to close the webview. Is there any way to do this? i.e. a way for the javascript in the webview to pass messages to the native app, and vice-versa?


share|improve this question

4 Answers 4

up vote 2 down vote accepted

Maybe have an NSNotification fire when an NSString instance field is changed. Use the post here to help understand how to communicate between JavaScript and Objective-C.

Then, it is just a matter of notification and string logic:

Steps for JavaScript to Objective-C:

  1. Web view button is pressed.
  2. JavaScript fires method which returns string.
  3. Local NSString is updated to denote button in Web view was pressed (see post above).
  4. NSNotification fires based on string change.
  5. Objective-C closes the Web view.

Not sure what you want to pass back to the Web view from Objective-C, but the above post should help.

share|improve this answer

You can consider using -webView:shouldStartLoadWithRequest:navigationType:. Create a dummy link which you can identify and close the web view. As for the other way round, @Paska's answer sounds appropriate.

share|improve this answer

You can attach any action using

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script

that detects the javascript from your webview!

Here is the doc: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/Reference/Reference.html

share|improve this answer

I think the best way is using pseudo image instead.

JS will insert a new image such as: http://example.com/callnative_1354524562763.gif?p1=v1&p2=v2

And your webview will override NSURLCache to retrieve 1x1 transparent image and parse parameters from you web content.

Note that NSURLCache only cache only one times, so please generate new timestamp for each calls.

This way is work for my project and call native iOS app immediately.

share|improve this answer

Your Answer


