iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
⛳

Understanding WebView and Native Code Integration in Strada

に公開
2024/05/26

Strada, the native component bridge library for the Turbo family—which I'm sure everyone loves—has finally been released, so I'll explore its implementation.

https://strada.hotwired.dev/

Prerequisites

  • Turbo Native is running the web app. This can be a remote URL.
  • Controllers are written using Stimulus.

Test Environment

You can try out Strada's features by loading https://turbo-native-demo.glitch.me/ with the Demo App from https://github.com/hotwired/turbo-ios.

git clone https://github.com/hotwired/turbo-ios
xed Demo/

The server-side code is available at https://github.com/hotwired/turbo-native-demo, and you don't need Rails to run it (it's written in Express+EJS).

For now, I'll explain using the https://turbo-native-demo.glitch.me/strada-menu sample.

If you open this page in the iOS app and tap a button, the UIAlertAction API of the native code is called outside the context of the WebView.

Usage

HTML for the View

https://github.com/hotwired/turbo-native-demo/blob/b119bf6f66d0fc87e7f45654e08857ab66c9edcd/views/strada-menu.ejs#L17-L68

The part declared with data-action="click@window->menu#hideOnClickOutside" is a view that falls back to the web only, not native.

Elements with data-action="click->bridge--menu#show click->menu#show" are for the native bridge.
They are attached via Stimulus.register("bridge--menu", BridgeMenuController).

https://github.com/hotwired/turbo-native-demo/blob/main/public/javascript/controllers/bridge/menu_controller.js#L1-L25

The this.send("display",... block is the key; by calling this, messages are sent to the native components.

There is a function for when a callback from native to web occurs. In this sample, it's the code to reflect the clicked menu item in the view.

Below is the implementation of the native component. send->onReceive() are paired. In this case, message.event='display'.

https://github.com/hotwired/turbo-ios/blob/main/Demo/Strada/MenuComponent.swift#L7-L54

When returning from native to the WebView, use reply().

The controller operating within the WebView is also running simultaneously, and the "was selected" message is displayed by the web side.

https://github.com/hotwired/turbo-native-demo/blob/main/public/javascript/controllers/menu_controller.js#L20-L23

You can also implement views outside the WebView natively. Below is the section describing the Android view.

https://github.com/hotwired/turbo-android/blob/bf945e18aed3d5749c9efc6e228d98bc012878af/demo/src/main/kotlin/dev/hotwire/turbo/demo/strada/MenuComponent.kt#L40-L60

https://github.com/hotwired/turbo-android/blob/bf945e18aed3d5749c9efc6e228d98bc012878af/demo/src/main/res/layout/menu_component_bottom_sheet.xml

Development Flow

That covers how to use Strada. The key point, as with the previous Hotwire series, is that it allows for gradual delivery in the following stages:

  1. Build a web app using only Turbo.
  2. Add Stimulus to write client-side logic.
  3. Turn it into a hybrid app with Turbo Native.
  4. Write bridge code with Strada to extend it.

Next, I want to dive deep into the internal implementation of Strada.

strada-ios Implementation

https://github.com/hotwired/strada-ios

  1. It extends WKWebView with WKUserContentController to attach the strada.js interface to the native side.
  2. window.Strada is injected into the browser side (web app), and a bidirectional messaging protocol using it is implemented.

So, if you execute code like this directly from the web app, it is possible to use messaging directly.

Strada.web.send({
  id: "1",
  component: "menu",
  event: "display",
  data: {
    metadata: { url: window.location.href },
    title: "Hello",
    items: [{ index: 0, title: "Goodbye" }],
  },
  callback: console.log('Strada callback'),
});

strada-android Implementation

https://github.com/hotwired/strada-android

  1. It sets StradaNative (strada.js) to window.nativeBridge using webview.addJavascriptInterface().
  2. It handles messaging by executing JavaScript code generated via webview.evaluateJavascript().

https://github.com/hotwired/strada-android/blob/d62653703b0b9527ef5ce162ed7ff414b73ffb8e/strada/src/main/kotlin/dev/hotwire/strada/Bridge.kt#L10-L29

https://github.com/hotwired/strada-android/blob/d62653703b0b9527ef5ce162ed7ff414b73ffb8e/strada/src/main/kotlin/dev/hotwire/strada/Bridge.kt#L100-L103

Discussion