There are three main modes of developing mobile apps: Native, Hybrid and Web apps.
One thing to keep in mind is that when choosing a development model, depending on your project type (Picture class?). Video class? News category? etc.), product business and personnel technology reserves to do trade-offs.
Hybrid development model is the development of both native and web apps. So how do we determine if a feature module in the app uses native or Web development? How can they be implemented if they need interface communication? And how to better maintain the hybrid app products?
1. Native or Web development module
When we choose to develop apps in hybrid mode, we should familiarize ourselves with the entire framework of the project and the various modules of the app. Then the generic, not so high performance requirements of the app interface can be extracted as a web interface development, native directly called. General hybrid app Development, the jump relationship between the app interface is implemented by native and completed, and the Web interface is primarily populated as content into browser Controls for Android and iOS. The Android client is using this mode before the native, which is built with an app framework shell that shows content and Web requests all implemented by the Web. Due to the different version of the client browser control, web interface loading, rendering and caching mechanisms, and so on, this model of the overall development of the app in the picture more than the circumstances of the experience is not very good, and may even appear unexpected wonderful problems. Recommended graphic list, involving video and other concurrency and interface elements compared to responsible or have a strong animation effect of the situation preferred native development. This is especially necessary in Android, where the browser control WebView before Android 4.4 is based on the default WebKit kernel, which differs from the WebKit kernel used by chromium and the implementation of WebView after 4.4 (including 4.4) is replaced with the chromium WebKit kernel. When we want to make the app as good as possible in the low version, we need to constantly do compatibility development and testing, unless you are in the app to package and compile the latest WebView source code, similar to developing a browser.
2. Native & Web Communication
Both Android and iOS provide direct calls to the API:
Webview.loadurl ("Javascript:functionname (\" "+ argument +" \ ")");
Don't forget to set Webview.getsettings (). Setjavascriptenabled (True);
[WebView stringbyevaluatingjavascriptfromstring:@ "alert (' Hello world! ')"];
- JS calls the native method.
The Android implementation JS Call native method is usually first written for the JS call class, and then by adding javascripteinterface methods, such as
Webview.addjavascriptinterface (Pandointerface, "Pando");
The Java object Pandointerface generated JS object Pando, and then directly Window.pando can call the Pandointerface object inside the method. Note that the Java method in the Pandointerface object for JS calls needs to be manually annotated with @javascriptinterface, which is introduced by Google to address security issues.
iOS implementation JS Call native method is relatively cumbersome. Primarily, iOS native does not provide the way JS calls native directly, only through the method of UIWebView related Uiwebviewdelegate protocol to intercept, and in this method, according to the URL of the protocol or feature string to do the call method or trigger the event work. When JS needs to call the native method, JS creates a hidden iframe setting or changes its SRC to trigger native to intercept the URL event.
Such as
-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype { *url = [request URL]; if ([[URL scheme] isequaltostring:@ "SomeFunction") { // Call the native method return NO; Else { return YES;} }
Do not know that we found no, the above two methods are only implemented JS call native method, but did not implement the JS function callback, the native method returns the result is passed to JS.
Open a lot of JSAPI interface, for everyone to call the function of native directly. Through the Jsapi interface and documentation we will find inside the implementation of the native method execution result callback to JS. This hybrid app development model is a good use of native high performance, multi-concurrency advantages, the network request, scan code and other complex logic or the web is not possible to achieve the function of native, and the web just do the interface display effect. After extracting the Android installation package, there is a 80 KB wxjs.js file in the Assets/jsapi directory, which implements the Jsapi logic Jsbridge. The following is a brief introduction to this Jsbridge implementation principle.
The key difficulty of Jsbridge is how to return the interface to JS after the native method is executed, and let JS understand the meaning of the transmitted parameters. For this, we can be in the JS call native method, JS once call the unique identifier of native identifier and expected native call after the execution of the callback function stored in the map, identifier and other parameters passed to native, After the completion of the native execution, the identifier and execution results are invoked as parameters to the JS function, parse the parameters in the JS function, get the identifier, and then query the corresponding callback function in the map, and pass the native execution result as the parameter in the call. A bit around, but the clever use of message delivery mechanism to implement JS call native callback function call, in the same way, native call JS function will return the result to native call is also possible.
Android in the implementation of Jsbridge, the early method also requires iOS, first to create an IFRAME to the Web interface, and then change the iframe by JS Src,native method through the shouldoverrideurlloading (WebView View, String URL) to intercept URL processing. But on Android 4.2.2 system, this method is changed in the iframe SRC, it is not triggered. At present, we recommend to intercept JS alert box to do the processing.
@OverridepublicBooleanOnjsalert (WebView view, string URL, string message, jsresult result) {logutils.i ("URL:" + url+ "\ n message:" +message); Try{String msg= Urldecoder.decode (Message, "Utf-8"); if(Msg.startswith (Bridgeutil.pando_return_data)) {//If you are returning datahandlerreturndata (message); Result.cancel (); return true; } } Catch(unsupportedencodingexception e) {e.printstacktrace (); } returnsuper.onjsalert (view, URL, message, result);}
Note that the Onjsalert method intercepts must be result.cancel after processing (); otherwise the web interface will appear without a click.
3. Hybrid App Maintenance
The Hybrid app involves web resource files such as HTML, CSS, and JS, and when there are calls between the web and native, any interface changes between the two will cause bugs in the app.
- Consider a similar scenario, the Jsbridge file or some of the common Web resources with the app package release, reduce maintenance costs, do not worry about the network is not loaded with the Web interface, the app before the interface upgrade is also stable available, of course, this also need to consider security issues, After all, your Web interface source code can be obtained through the anti-compilation.
- All Web resources are obtained uniformly from the server. Because the Web resource is on the server, the Web interface can be changed dynamically at any time, and the bug is not published, and the direct online interface is upgraded. But to maintain web resources, each version of the app that is published is available when the Web resource is loaded.
Discuss:
Q:
Recently want to write an Android hybrid app, see the next most recommended is PhoneGap, do not know how his operating efficiency, if the original first, suitable for phonegap?
A:
I say, in fact, MUI Development is the fastest, this is the result of my own practice, ionic me useless, but Cordova I used, because there are not many people to share experience, so many pits.
You do a sweep code, shake, geo-tracking, message push app, these basic features MUI basic without research to be able to use. Cordova just study plug-ins integration and use will take 3 days, there will be a lot of holes in the middle. (This already has a semi-finished product)
However, Cordova is good for you to make a more exotic app, because his plugin library is full. MUI is weaker for this support. (this test gave up after a week)
Then again, if so peculiar demand of the app in the technology selection why not choose the native? If you have to use hybird yourself to do with WebView. Learn Cordova time enough to make a native call JS and JS call native to understand 10 times. That's what I'm doing now.
A:
If the native part of the function is higher, it is not recommended to use PHONEGAP, although it is now very mature, but if it involves the native function is very poor, I have experienced before.
---------------------------------------Cut-------------------------------------
The above content is excerpted from
Https://www.tuicool.com/articles/riE3Yn
Thank the great gods for their practical summary ~
Hybrid APP Development Model