Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native -> JS communication in WKWebView IFrames

In WKWebView we can call ObjectiveC/Swift code using webkit message handlers webkit.messageHandlers.<handler>.pushMessage(message).

And this works for IFrames too.

Is there a way to respond to this by calling into the IFrame?

Currently the evaluateJavaScript: API supports communication only to the main frame.

like image 390
shreyasva Avatar asked Jan 28 '16 21:01

shreyasva


People also ask

What is the difference between UIWebView and WKWebView?

One major architectural difference between UIWebView and WKWebView is that the methods of WKWebView tend to be asynchronous, while the methods of UIWebView were synchronous. This difference requires code and architecture changes in your app.

What is WKUserContentController?

A WKUserContentController object provides a bridge between your app and the JavaScript code running in the web view. Use this object to do the following: Inject JavaScript code into webpages running in your web view. Install custom JavaScript functions that call through to your app's native code.

What browser does WKWebView use?

WKWebView uses the Nitro JavaScript engine, also used by mobile Safari, which comes with significant performance improvements over UIWebView's older JavaScript engine.


2 Answers

What you need is a JavaScript bridge to IOS take a looik at : http://www.priyaontech.com/2014/12/native-%E2%80%93-js-bridging-on-ios8-using-wkwebview/ and http://www.joshuakehn.com/2014/10/29/using-javascript-with-wkwebview-in-ios-8.html

Hope this helps

like image 196
tnt-rox Avatar answered Oct 20 '22 12:10

tnt-rox


Here is a real good library for a Javascript bridge (it supports UIWebview and WKWebview) : https://github.com/marcuswestin/WebViewJavascriptBridge

The bridge can be used for native to JS or JS to native communication. They've made quite a good job you should have a look on it.

On web side you have to add some JavaScript code, so that the bridge is present on HTML side, their doc shows a setupWebViewJavascriptBridge method to add (Read Usage part of their readme). In the end it up to you wether you add this code directly in the page, or through iOS using evaluateJavaScript

like image 2
Niko Avatar answered Oct 20 '22 13:10

Niko