Any way for web browsing to communicate with native iOS app?

I have an iPad app that contains web browsing. There is a button on the web view page in which, when clicked, you need to somehow tell the native application to close the web view. Is there any way to do this? that is, a way for javascript in webview to pass messages to a native application and vice versa?

Thanks!

+7
source share
4 answers

Perhaps there is an NSNotification fire when changing the field of an NSString instance. Use the message here to understand how to communicate between JavaScript and Objective-C.

Then this is just a matter of notification and string logic:

Steps for JavaScript to Objective-C:

  • The web browsing button is pressed.
  • JavaScript runs a method that returns a string.
  • The local NSString is updated to indicate the button in the web view has been clicked (see above).
  • NSNotification is triggered based on a line change.
  • Objective-C closes the web view.

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

+2
source

You can use -webView:shouldStartLoadWithRequest:navigationType: Create a dummy link that you can identify and close the web view. Conversely, @Paska's answer sounds appropriate.

+4
source

You can attach any action using

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

which detects javascript from your website!

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

0
source

I think the best way is to use a pseudo-image.

JS will add a new image, for example: http://example.com/callnative_1354524562763.gif?p1=v1&p2=v2

And your NSURLCache override NSURLCache to get 1x1 transparent image and analysis parameters from your web content.

Please note that NSURLCache cached only once, so please create a new timestamp for each call.

This method works for my project and immediately launches the application for native iOS.

0
source

All Articles