Sådan omdannes dit websted til en mobilapp med 7 linjer med JSON

En ny tilgang til blanding af webmotor i native apps

Hvad hvis jeg fortalte dig de 7 linjer med JSON ovenfor, farvet i orange, er alt hvad du behøver for at gøre et websted til en mobilapp? Ingen grund til at omskrive dit websted ved hjælp af en ramme-API bare for at få det til at opføre sig som en mobilapp. Bare tag dit eksisterende websted, som det er, og bland det i en indbygget app med en simpel URL-reference.

Og hvad hvis du bare ved at tilpasse JSON-markeringen en smule, kan få adgang til alle de oprindelige API'er, indbyggede UI-komponenter såvel som native view-overgange ud af kassen?

Sådan ser et minimalt eksempel ud i aktion:

Læg mærke til, hvordan jeg har integreret en github.com-webside, men resten af ​​layoutet er alle indbyggede UI-komponenter, såsom navigationsoverskriften og den nederste fanelinje. Og overgangen er automatisk hjemmehørende uden at du behøver at omskrive webstedet ved hjælp af API'er.

Inden jeg forklarer hvordan, kan du spørge: “Det er sejt, men kan du gøre noget meningsfuldt andet end bare at vise websiden i en indbygget appramme?”

Fantastisk spørgsmål, fordi det er hovedemnet for dette indlæg. Alt hvad du skal gøre er at oprette en sømløs 2-vejs kommunikationskanal mellem webvisningen og appen , så den overordnede app kan udløse alle JavaScript-funktioner inde i webvisningen, og webvisningen kan nå udefra for at kalde native API'er.

Her er et sådant eksempel:

Bemærk, at denne visning indeholder:

  1. Native navigationshoved, komplet med indbygget overgangsfunktionalitet
  2. A Web view, which embeds a QR code generator web app
  3. A native chat input component at the bottom

All this can be described by just tweaking some of the JSON markup attributes we saw above.

Finally, note that the QR code changes as you enter something from the chat input. The chat input triggers a JavaScript function inside the QR code web app that re-generates the image.

No app development framework has tried to fundamentally solve this problem of “seamless integration of web view into native apps” because they’re all focused on picking either 100% native or 100% HTML5 side.

Whenever you hear someone talk about the future of mobile apps, you would probably hear them talk about “Will it be the HTML5 approach that wins out? Or will it be native?”

None of them see native and html as something that could co-exist and furthermore, create synergy and achieve things that are not easily possible otherwise.

In this article I’m going to explain:

  • Why blending web engine and native components is often a good idea.
  • Why a seamless integration of HTML and Native is not easy, and how I implemented one.
  • Most importantly, how YOU can use it to build your own app instantly.

Why would you use HTML in a native app?

Before we go further, let’s first discuss whether this is even a good idea, and when you may want to take this approach. Here are some potential use cases:

1. Use Web Native Features

Some parts of your app may be better implemented using the web engine. For example, Websocket is a web-native feature that’s designed for the web environment. In this case it makes sense to use the built-in web engine (WKWebView for iOS and WebView for Android) instead of installing a 3rd party library that essentially “emulates” Websocket.

No need to install additional code just to do something that you can do for free, which brings us to the next point.

2. Avoid Large Binary Size

You may want to quickly incorporate features that will otherwise require a huge 3rd party library.

For example, to incorporate a QR code image generator natively, you will need to install some 3rd party library which will increase the binary size. But if you use the web view engine and a JavaScript library through a simple