Cross-terminal web hybrid app development comparison

Source: Internet
Author: User

The product of the hybrid development of the Native app (hereinafter referred to as Native) and mobile Web (hereinafter referred to as Web) is called the Hybrid app (hereinafter referred to as hybrid). Hybrid is not a new concept, it can be traced back to the Symbian era until the advent of iOS and Android to fully demonstrate value.

A brief history of hybrid
Hybrid not only leverages the native app's rich device API, but also has the ability to cross-platform, efficiently develop, and quickly publish mobile web, which is appropriate for a very large application scenario.

The hybrid advantage lies in:

Cross-platform
Web content can be developed once, all platforms are in effect, and many products require this ability.

Quick release
iOS platform, the average Apple store audit cycle of 1-2 weeks, or even longer, product release cycle from 2 weeks to January, which is difficult to accept products that need to be released quickly.

Android platform, the App Store is numerous, the publishing process is cumbersome. While an in-app upgrade is possible, the problem is that the new app needs to pass through the App Store, and the apk is bulky and 2g/3g in a poor environment.

Efficient development
Web development after 20 years of development, has been the structure (HTML), Performance (CSS), Behavior (JavaScript) 3 parts very well separated, in the Division of Labor collaboration, development efficiency will have a clear advantage.

A rich device API
The Web (HTML5) emphasizes versatility and is limited by standards and browser implementations, and many useful system functions are not supported (or partially supported). The biggest advantage of native is the ability of the device API to call, as long as bridging native and Web,web can.

Hybrid inferior performance is:

Cpu/gpu intensive applications currently look more suitable for native, such as the need for speed games. This disadvantage is constantly weakening, as the "CSS Transform 3D" introduced into the GPU greatly alleviates the problem of web animation is not smooth.
The UI presentation latency problem caused by static resources loading from the server side. This problem can be mitigated by loading a packaged public library by native intercept WebView traffic.

From the latest domestic data can be seen, the trend of hybrid is also very obvious. As you can see from figure 8-1, more and more developers are deciding to use hybrid (cross-platform technology), with a total of 54% in the last two years, while nearly 60% of developers have chosen PhoneGap for hybrid's technical solutions.

Hybrid technology

Whether Android or iOS, implementing a simple hybrid app requires just a few lines of code: instantiating WebView, loading the page, and then the code of the page itself. It also requires a lot of knowledge to achieve more complex and complete hybrid.

Mobile Web Development Basics: HTML, CSS, JavaScript.
Native App Development Basics: Android, IOS.
Native and web two-way communication mechanism.

Next, native and web two-way communication mechanism:
1. Native Call web
Whether Android or iOS, the native call Web (JavaScript) has good native support.
Android calls JavaScript

webView.loadUrl("javascript:(function(){ alert(‘ok’); })()”);

iOS calls JavaScript

[webView stringByEvaluatingJavaScriptFromString: @"alert(‘ok‘)" ];

2. Web Call native
"Native Call web" is essentially a dynamic execution of JavaScript scripts, and in the context of "web invoke native", the current native language (Java and objective-c) is not as easy to execute dynamically as JavaScript, So we need to take a path.
2.1 Android
There are 3 common ways to do this on Android.
1. Rewrite webviewclient.shouldoverrideurlloading

webView.setWebViewClient(new WebViewClient(){        @Override        publicboolean shouldOverrideUrlLoading (WebView view, String url){            // TODO解析URL并触发Native代码         returntrue;        }    });

When a URL in a page changes, such as clicking a link, executing a javascript (such as location.href= "/http" The webviewclient.shouldoverrideurlloading is triggered by encapsulating the data of the Web invocation native in the URL, and then by native parsing the data and executing the response native method.
2. Rewrite webchromeclient.onjsprompt, or onjsconfirm, or Onjsalert, take webchromeclient.onjsprompt for example.
Overriding webchromeclient.onjsprompt

webView.setWebChromeClient(new WebChromeClient() {        publicStringStringString defaultValue, JsPromptResult result) {        // TODO解析message并触发Native代码                     result.confirm("");            returntrue;        }    });

Webchromeclient.onjsprompt is triggered when the JavaScript code such as "Window.prompt (" {} ")" is executed.
3.webview.addjavascriptinterface, this approach differs from the first two, by mapping Java object (A) to JavaScript object (B), The A.FUNC1 is automatically triggered when the b.func1 is called, and the "Web call native" is implemented in this native way.

webView.addJavascriptInterface(new Object() {        publicvoid func1() {        }        publicvoid func2() {        }    "webViewObj");

Author Kai Xu released on May 27, 2015 | Note: GMTC Global mobile technology Conference June 2016 24-25th, learn more! 2 Discussions
Share to: Weibo Facebook Twitter Youdao Cloud Note email sharing
Read my reading list later
Editor's note: Infoq opens a new column "Taste book", a selection of wonderful chapters of technical books, as well as the sharing of reading books left behind the thinking and harvest, welcome attention. This article extracts from Kai Xu "Hybrid app" in the eighth chapter of "Cross Terminal Web", mainly describes the development of Hybrid app and the technology realization, and finally introduces two main Hybrid development frameworks Phonegap/cordova and titanium.

The product of the hybrid development of the Native app (hereinafter referred to as Native) and mobile Web (hereinafter referred to as Web) is called the Hybrid app (hereinafter referred to as hybrid). Hybrid is not a new concept, it can be traced back to the Symbian era until the advent of iOS and Android to fully demonstrate value.

A brief history of hybrid

    1. Background

Hybrid not only leverages the native app's rich device API, but also has the ability to cross-platform, efficiently develop, and quickly publish mobile web, which is appropriate for a very large application scenario.

The hybrid advantage lies in:

Cross-platform
Web content can be developed once, all platforms are in effect, and many products require this ability.

Quick release
iOS platform, the average Apple store audit cycle of 1-2 weeks, or even longer, product release cycle from 2 weeks to January, which is difficult to accept products that need to be released quickly.

Android platform, the App Store is numerous, the publishing process is cumbersome. While an in-app upgrade is possible, the problem is that the new app needs to pass through the App Store, and the apk is bulky and 2g/3g in a poor environment.

Efficient development
Web development after 20 years of development, has been the structure (HTML), Performance (CSS), Behavior (JavaScript) 3 parts very well separated, in the Division of Labor collaboration, development efficiency will have a clear advantage.

A rich device API
The Web (HTML5) emphasizes versatility and is limited by standards and browser implementations, and many useful system functions are not supported (or partially supported). The biggest advantage of native is the ability of the device API to call, as long as bridging native and Web,web can.

Hybrid inferior performance is:

Cpu/gpu intensive applications currently look more suitable for native, such as the need for speed games. This disadvantage is constantly weakening, as the "CSS Transform 3D" introduced into the GPU greatly alleviates the problem of web animation is not smooth.
The UI presentation latency problem caused by static resources loading from the server side. This problem can be mitigated by loading a packaged public library by native intercept WebView traffic.
2. Brief History

Prototype
The embryonic stage is roughly:

Symbian V3/5 era has hybrid embryonic form.
iOS's original app was written by Objective-c, the limited store's release cycle, and the often-changing portions of the content started by loading the server-side page with the built-in browser control (WebView).
After the advent and popularity of Android, more app functionality can be implemented through hybrid, so that only one version can be maintained on different platforms.
Development
"Cross-platform" has become hybrid's biggest selling point, with Phonegap[1] led by the hybrid framework has emerged, bringing a lot of changes.

Access device features.
The functionality that the Web (HTML5) does not support can allow native to be implemented, and then communicate between native and the web, in such a way that the web obtains and native the same device API invocation capabilities, which is the basic workings of phonegap such hybrid frameworks.
At the same time, the hybrid framework (such as tianium[2]) that turns web code into native also appears.
PhoneGap Sub-project weinre is a kind of remote debugging tool, which greatly alleviates the problem that hybrid is difficult to debug, and further promotes the development of hybrid.
The hybrid framework provides app packaging capabilities that developers can use to develop native apps entirely using HTML, CSS, and JavaScript.
Mature
With the phonegap of such hybrid framework in the global epidemic, some problems have been exposed, it is the solution of these problems, let hybrid mature.

Development experience improvements.
Weinre such debugging tools are still plug-in nature, such as "Network", "Local resources" and other advanced debugging features can not be supported, WebView native debugging needs more and more intense.
IOS 6.0+ has support for native remote debugging [3].
[4] Chrome for Android is a leader in native remote debugging.
Starting with Android 4.4, WebView also supports native remote debugging [5].
There is an increasing demand for enhanced webview performance.
Some features that pursue extreme performance are implemented by native, such as transitions (inter-page switching) animations.
Static resource localization is ideal, and other scenarios native intercept WebView requests and redirect public resources to the app's built-in resources to speed up the web.
3. Status Quo

The above is hybrid development overview, from the latest domestic data can be seen, the trend of hybrid is also very obvious. As you can see from figure 8-1, more and more developers are deciding to use hybrid (cross-platform technology), with a total of 54% in the last two years, while nearly 60% of developers have chosen PhoneGap for hybrid's technical solutions.

Figure 8-1 The development of hybrid in China [6]

Of the 2,309 mobile developers surveyed, only 8% of the total use of native was developed by August 2013, while the remaining 92% per cent could be considered to be used as shown in Hybrid,8-2.

Figure 8-2 Usage of hybrid

The cross-platform nature of the app becomes an important consideration, as shown in 8-3.

Figure 8-3 Cross-platform features are being watched

Figure 8-4 shows hybrid's astonishing growth rate: in 2013, hybrid (or HTML apps), both developed and published, grew by more than 125%~400% in 2012 [8].

Figure 8-4 Hybrid growing rapidly [9]

Hybrid technology

Whether Android or iOS, implementing a simple hybrid app requires just a few lines of code: instantiating WebView, loading the page, and then the code of the page itself. It also requires a lot of knowledge to achieve more complex and complete hybrid.

Mobile Web Development Basics: HTML, CSS, JavaScript.
Native App Development Basics: Android, IOS.
Native and web two-way communication mechanism.
Mobile Web Development basis can refer to the 2nd chapter of this book, Native app Development Foundation has gone beyond the scope of this book, there are many optional books, this section to the remaining 3rd question "Native and web two-way communication mechanism."

    1. Native calling the Web

Whether Android or iOS, native call Web (JavaScript) has good native support, as shown in code 8-1 and code 8-2. The call method in Android is as follows, where WebView is an instance of WebView.

Code 8-1 Android Call JavaScript

Webview.loadurl ("javascript: (function () {alert (' OK ');} )()”);
The call method in iOS is as follows, where WebView is an instance of UIWebView.

Code 8-2 iOS Call JavaScript

[WebView stringbyevaluatingjavascriptfromstring: @ "alert (' OK ')"];
2. Web Call native

"Native Call web" is essentially a dynamic execution of JavaScript scripts, and in the context of "web invoke native", the current native language (Java and objective-c) is not as easy to execute dynamically as JavaScript, So we need to take a path.

2.1 Android
There are 3 common ways to do this on Android.

Rewrite the webviewclient.shouldoverrideurlloading (as shown in Code 8-3).
Code 8-3 overriding webviewclient.shouldoverrideurlloading

Webview.setwebviewclient (New Webviewclient () {
@Override
public boolean shouldoverrideurlloading (WebView view, String URL) {
Todo parses the URL and triggers the native code
return true;
}
});
When the URL in the page changes, such as clicking on a link, executing JavaScript (such as location.href= "/http" The webviewclient.shouldoverrideurlloading is triggered by encapsulating the data of the Web invocation native in the URL, and then by native parsing the data and executing the response native method.

Rewrite webchromeclient.onjsprompt, or onjsconfirm, or Onjsalert, to webchromeclient.onjsprompt as an example, as shown in code 8-4.
Code 8-4 overriding Webchromeclient.onjsprompt

webView.setWebChromeClient(new WebChromeClient() {    public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {    // TODO解析message并触发Native代码                 result.confirm("");        return true;    }});

Webchromeclient.onjsprompt is triggered when the JavaScript code such as "Window.prompt (" {} ")" is executed.

Webview.addjavascriptinterface, this approach differs from the first two, by mapping Java object (A) to JavaScript object (B), The a.func1 is automatically triggered by invoking B.FUNC1, which implements the "Web call native" in this native way, as shown in code 8-5.
Code 8-5 Webview.addjavascriptinterface

webView.addJavascriptInterface(new Object() {    public void func1() {    }    public void func2() {    }}, "webViewObj");

The above 3 ways, the most commonly used is the way 2; Mode 2 has built-in queue support compared to mode 1, there is no case of high-frequency access data loss, and Mode 3 is the native mode of Android, but not as flexible as the first two methods.

2.2 IOS
iOS is available in a way similar to webviewclient.shouldoverrideurlloading in Android, enabling web invocation native by monitoring WebView URL changes.

- (BOOL)webView:(UIWebView *) webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType: (UIWebViewNavigationType)navigationType {}

Hybrid frame

Currently a hybrid framework typically provides the following functionality.

Device API: Encapsulates the functionality of native and provides a consistent device API across platforms.
App Packaging: Package The code written by HTML5 as an app (titanium translates the Code).
PhoneGap almost become synonymous with hybrid, titanium and phonegap the design concept of a large difference, the image of the PhoneGap and titanium components.

    1. PhoneGap

1.1 PhoneGap and Cordova
[12] PhoneGap developer Notibi The PHONEGAP code to the Apache Software Fund (ASF) in 2010, PhoneGap core engine became a new open source project Cordova and phonegap a release version of Cordova. In October 2011, Notibi was acquired by Adobe [13], but did not affect the open source nature of PhoneGap and Cordova.

1.2 Principle

"One place to write, run in multiple places", the main functions of phonegap are:

Provides hybrid APIs that can be directly called by JavaScript such as acceleration, camera, compass, GPS, contacts and other system-level APIs, complete API list please visit the PhoneGap API Reference.
Content developed using the Web (HTML, CSS, JavaScript) is PhoneGap compiled into native apps packaged as individual platforms.

1.3 Classic Cases

Data from PhoneGap showcase[14] and other data sources are displayed:

Facebook Mobile sdk[15] and Salesforce Mobile sdk[16 are all developed based on the Cordova branch.
The web code in the Facebook client is more than 90%[17].
The web code in the LinkedIn ipad client is even more than 95%.
Wikipedia has developed its own ios/android Hybrid app[18] directly with PhoneGap, [19] and open source code on GitHub.

Cross-terminal web hybrid app development comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.