Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way for webview to communicate with native iOS app?

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!

like image 421
blabus Avatar asked May 18 '11 15:05

blabus


People also ask

How do you communicate between WebView and native Android?

2 — Android: 2.1 To receive data from webview ,we can create an interface, which will enable webview to connect the native layer and pass data. From native layer, create a class and replicate the following. While configuring web view, we need to set JavaScript interface as above JSBridge class.

How do you communicate with data between WebView in react native?

Import the React Native WebView package in App. js . We will create a normal HTML page in WebView and use “script” tags to let the HTML page communicate with the React Native app. In the “script” tag, we are going to call a sendDataToReactNativeApp() function in which we will use a window property called window.

What can I use instead of WebView?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.


2 Answers

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.

like image 164
Deepak Danduprolu Avatar answered Oct 26 '22 19:10

Deepak Danduprolu


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.

like image 20
Jacob Barnard Avatar answered Oct 26 '22 18:10

Jacob Barnard