On my work, technology and related stuff....


In iOS8 Apple introduced the Modern WebKit framework. The Modern WebKit supports a multi-process architecture wherein web content is loaded in a process separate from the app (upto a limit). The framework is unified across the iOS and OS X platforms and includes a lot of performance enhancements such as the optimized JS Nitro Engine , hardware accelerated 60fps scrolling etc. It also includes support for WebGL for 3D rendering.

The WKWebView For Displaying Web Content

The WKWebView is a replacement for the UIWebView on iOS for loading web content within a native app. It is essentially a wrapper around the WebKit.framework and exposes an interface that is a lot more powerful than what the UIWebViews allowed.

One of the significant improvements is the simplified communication model between Native and JS .
This article will discuss how Native-WebView bridging can be achieved.

WKWebViews are associated with a WKWebViewConfiguration object. Multiple webviews can share the configuration object.

The following code snippet creates an instance of WKWebView


Native to JS Bridging Using User Scripts.

User Scripts are JS that you inject into your web page at either the start of the document load or after the document is done loading. User scripts are extremely powerful because they allow client-side customization of web page, allow injection of event listeners and can even be used to inject scripts that can in turn call back into the Native app.
The following code snippet creates a user script that is injected at end of document load. The user script is added to the WKUserContentController instance that is a property on the WKWebViewConfiguration object.



JS to Native Bridging Using Script Messages

Your web page can post messages to your native app via the
window.webkit.messageHandlers.<name>.postMessage (<message body>) method.
Here,  “name” is the name of the message being posted back. The JS can post back any JS object as message body and the JS object would be automatically mapped to corresponding ObjC / Swift native object.
The following JS code snippet posts back a message when a button click event occurs on a button with Id “ClickMeButton”


Handling Callbacks Using Script Message Handlers

In order to receive messages posted by your web page, your native app needs to implement the WKScriptMessageHandler protocol.
The protocol defines a single required method. The WKScriptMessage instance returned in the callback can be queried for details on the message being posted back.

ObjC :


Finally, the native class that implements WKScriptMessageHandler protocol needs to register itself as a message handler with the WKWebView as follows.



Sample Code:

A complete example app demonstrating the bridging concept can be downloaded from https://github.com/rajagp/iOS-WKWebViewBridgeExample-ObjC.git. The Swift Version can be downloaded from https://github.com/rajagp/iOS-WKWebViewBridgeExample-Swift.git.

In this sample app, we load a simple HTML page that has a button using a WKWebView. When the page loads, the native app injects a JS (“User Script”) into the loaded document that listens for button click event and calls back into the native app (“Script Message”) . The native app implements a listener to handle the callback message from the web page and updates the color of the button from within the callback handler

I did a short presentation on this topic at CocoaHeads. The presentation can be downloaded from here.

Tags: , , , , , , ,


  1. Sibasish
  2. breal

Trackback e pingback

No trackback or pingback available for this article

Leave a Reply