8

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?

Thanks!

edc1591
  • 10,146
  • 6
  • 40
  • 63
blabus
  • 795
  • 2
  • 14
  • 25

4 Answers4

4

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.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
2

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.

Community
  • 1
  • 1
Jacob M. Barnard
  • 1,347
  • 1
  • 10
  • 24
0

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.

KimKha
  • 4,370
  • 1
  • 37
  • 45
0

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

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
elp
  • 8,021
  • 7
  • 61
  • 120