WKWebViewinstance (references as webView in the following example code) and
WKScriptMessageHandlerprotocol implementation (
JSMessageHandlerin example). PWA should provide a UI and send messages using Web Kit API. This API is injected by
WKWebViewitself and is accessed by calling a function in a following manner:
JSMessageHandlerclass. It consists of the two parts. First one is implementation details and it declares constants with message names and declares two closure properties which should be called when a message from the PWA side is received.
userContentControllerfunction and this is the
WKScriptMessageHandlerprotocol function. It’s called when a message to Web Kit API is sent from the web side.
WKScriptMessageis passed as an argument and this object contains the actual message name and arguments passed to it. It’s a
WKScriptMessageHandlerresponsibility to identify the message and convert the argument passed if needed. In the following example it’s achieved by using a switch operator and calling a corresponding closure if message is identified.
ViewControllerclass which creates a
JSMessageHandlerinstance and configures
WKWebViewinstance with it. Following function creates
WKUserContentControllerinstance created. Afterwards a
JSMessageHandlerinstance is created and its actions are populated. In this example these actions just report to the console and then call
window.exampleActionWithArgumentCallback()correspondingly. It’s suggested that these functions are added by PWA and it will process the calls to them. In real world project this process most likely will be asynchronous and native code will call back to PWA after user interaction (getting QR code, accessing contacts etc)
WKWebViewConfigurationis created, configured and returned afterwards.
WKWebViewinstance is created as follows and should open PWA by URL later. The rest of the code is a standard UIKit / WebKit and is skipped for clarity. See attached example project. It’s configured to open a non-existing "https://your.pwa.com" site. You can change it to your own PWA URL.