Deploying Native Apps
In order to publish a PWA as a stand alone native application in the App Store it’s required to add extra functionality which is not available by opening PWA in a web browser. Allowing to receive push notifications, scan QR codes, and access user contacts might be such features.
To achieve this goal it’s required to create a native iOS app containing WKWebView instance (references as webView in the following example code) and WKScriptMessageHandler protocol implementation (JSMessageHandler in example). PWA should provide a UI and send messages using Web Kit API. This API is injected by WKWebView itself and is accessed by calling a function in a following manner:
1
window.webkit.messageHandlers.<message_name>.postMessage("Hello, native world!");
Copied!
Example project is configured to respond to following messages: exampleMessage, exampleMessageWithArgument
To invoke them in the example project it’s required to make following function calls on PWA side:
1
window.webkit.messageHandlers.exampleMessage.postMessage();
2
window.webkit.messageHandlers.exampleMessageWithArgument.postMessage("Hello, native world!");
Copied!
Returning to the native code of the example project let’s take a look at the JSMessageHandler class. 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.
Second part is the userContentController function and this is the WKScriptMessageHandler protocol function. It’s called when a message to Web Kit API is sent from the web side. WKScriptMessage is passed as an argument and this object contains the actual message name and arguments passed to it. It’s a WKScriptMessageHandler responsibility 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.
1
class JSMessageHandler: NSObject, WKScriptMessageHandler {
2
struct MessageNames {
3
static let exampleMessage = "exampleMessage"
4
static let exampleMessageWithArgument = "exampleMessageWithArgument"
5
}
6
var exampleAction: (() -> Void)?
7
var exampleActionWithArgument:((String) -> Void)?
8
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
9
switch message.name {
10
case MessageNames.exampleMessage:
11
exampleAction?()
12
case MessageNames.exampleMessageWithArgument:
13
if let argument = message.body as? String {
14
exampleActionWithArgument?(argument)
15
} else {
16
print("Wrong argument type for message \(message.name) : \(message.body)")
17
}
18
default:
19
print("Unknown message: \(message.name)")
20
}
21
}
Copied!
Second class of the example project is a ViewController class which creates a JSMessageHandler instance and configures WKWebView instance with it. Following function creates WKWebViewConfiguration instance:
1
func createWebViewConfiguration() -> WKWebViewConfiguration {
2
let contentController = WKUserContentController()
3
let handler = JSMessageHandler()
4
handler.exampleAction = { [weak self] in
5
print("exampleMessage was sent from PWA and is handled in native code")
6
self?.webView.evaluateJavaScript("window.exampleActionCallback()", completionHandler: nil)
7
}
8
handler.exampleActionWithArgument = { [weak self] argument in
9
print("exampleMessageWithArgument '\(argument)' was sent from PWA and is handled in native code")
10
self?.webView.evaluateJavaScript("window.exampleActionWithArgumentCallback('\(argument + " Hello from native code!")')", completionHandler: nil)
11
}
12
contentController.add(handler, name: JSMessageHandler.MessageNames.exampleMessage)
13
contentController.add(handler, name: JSMessageHandler.MessageNames.exampleMessageWithArgument)
14
let config = WKWebViewConfiguration()
15
config.userContentController = contentController
16
return config
17
}
Copied!
First of all there is a WKUserContentController instance created. Afterwards a JSMessageHandler instance is created and its actions are populated. In this example these actions just report to the console and then call window.exampleActionCallback() or 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)
Afterwards the content controller is instructed to pass the message handling for each expected message to create a JSMessageHandler instance. WKWebViewConfiguration is created, configured and returned afterwards.
1
lazy var webView: WKWebView = {
2
let wv = WKWebView(frame: .zero, configuration: createWebViewConfiguration())
3
Copied!
WKWebView instance 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.
TutotialPWA.zip
32KB
Binary
TutorialPWA.zip
Last modified 6mo ago
Copy link