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.
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.
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.
WKWebView uses the Nitro JavaScript engine, also used by mobile Safari, which comes with significant performance improvements over UIWebView's older JavaScript engine.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With