Skip to content

register(_:webViewClass:configure:attach:)

Registers the hook for key.

static func register(_ key: String, webViewClass: WKWebView.Type? = nil, configure: ((WKWebViewConfiguration, WebViewContext) -> Void)? = nil, attach: @escaping (WKWebView, WebViewContext) -> Void)

Parameters

key

The key of the app’s extension that turns on the hook. Keys are global, so prefix yours with your package’s name.

webViewClass

The WKWebView subclass that each WebView enabling this hook is made from, to override what WKWebView lets a subclass override; nil leaves the class to the other hooks. When the enabled hooks name different classes, the first one’s in registration order makes the view, and a warning names the others. Those still attach, so check the class attach gets. The hidden view that window.open('') gets is always a plain WKWebView.

configure

Runs before the WKWebView exists, with the configuration it is created from: after the ios.configuration settings and before the bridge, so replacing the user content controller is fine. User scripts you add stay, and run after the package’s. It gets the same context that attach gets next, so state it makes can be keyed by the context. window.open('') opens a hidden WKWebView from this configuration: your user scripts run in it and your script message handlers get its messages, so compare message.webView with the WKWebView attach gets.

attach

Runs once the WKWebView exists and its settings are applied, so what it sets wins over them. Register what to run when the view goes away with onDestroyed(_:). A renderer crash reloads the same WKWebView, so nothing attaches again (see DocumentEnd.crashed).

Discussion

Call it from your module’s OnCreate, before the first WebView.

A hook under a key that is taken already replaces that hook in its place, with a warning, so a key collision doesn’t stop the app. A hook under core is logged and not registered.

Hooks are configured and attached in registration order, which across packages is autolinking’s: packages sorted by name. A hook whose key turns on after the WebView was created attaches after the hooks attached already.