Project implementation of the "Quickhybrid" iOS side

Source: Internet
Author: User

Objective

18 New Year's Day in three days and friends assault, reluctantly is to make the prototype, follow-up API slowly perfect. (Of course, the main force or that friend, I just first involved in iOS, barely able to read, fix changes to change it)

The general contents are as follows:

    • Jsbridge Core Interaction Section

    • ui, and other page common API implementations (others slowly improve)

    • Implementation of component (custom) API expansion

    • The API's permission check only reserved one entry, simulating the simplest implementation

    • Other mechanisms such as offline resource loading update, underlying optimization, etc. are not available for the time being

Structure of the project

In this project, for convenience, it is not partitioned into multiple static libraries (which in fact can be done), but all are placed in one project

The overall directory structure is as follows:

quickhybrid-ios    |- AppDelegate          // 应用入口,分发进入对应的viewcontroller    |- core                 // 核心工具类,放一些通用工具类    |   |- ui    |   |- util    |   |- ...    |- quickhybrid          // JSBridge实现的核心代码,定制viewcontroller,实现API等    |   |- WebViewJavascriptBridge    |   |- basecore    |   |- quickcore    |   |- api

Code schema

Like Android, it's still a simple three-time architecture:底层核心工具类->JSBridge桥接实现->app应用实现

where core and Jsbridge can be packaged as static libraries if necessary

Core|-Ui//The definition and implementation of some UI effects    |-Util//General tool classQuickhybird|-Webviewjavascriptbridge//Third party open source Jsbridge implementation, which has been modified    |-Basecore//define base class Viewcontroller    |-Quickcore//define the Viewcontroller implementation in Quickhybrid    |-Api//Define API, open native function to H5In-app|-Appdelegate//Application portal, distribution into the corresponding Viewcontroller    |-Mainviewcontroller//Portal Interface    |-Testpayapi//Define a Test payment component (custom) API    |- Qhjsmodules.plist            //Internal definition module alias in the path relationship configuration file
Permissions Configuration

iOS can configure permissions directly in Info.plist, such as

    <Key>Nsapptransportsecurity</key><dict><key>nsallowsarbitraryloads</key>        <true/>    </dict><key>nscamerausagedescription</key>    <String>Do you want to allow the app to use webcam?</string><key>nslocationwheninuseusagedescription</key>    <String>Whether to allow apps to use the targeting feature</string><key>nsmicrophoneusagedescription</key>    <String>Do you want to allow the app to use a microphone?</string><key>nsphotolibraryusagedescription</key>    <String>Whether to allow access to albums</string><key>uifilesharingenabled</key>...
Application Configuration
Bundle Identifier:com.quickhybrid.quickhybriddemoVersion:1.0.0Deployment Target:11.2(默认最新调试)Devices: UniversalSigning: none

Compared to a bunch of complex Android configurations, iOS is undoubtedly a lot simpler, directly with the latest system debugging can ...

Here, to the current location, there are a lot of APIs that are not implemented, and subsequent projections are expected to introduce some static libraries.

Of course, if you want to introduce a static library, it's also very simple, directly as follows:

项目配置->Build Phases->Link Binary With Libraries->+(添加)->然后需要用到的地方import即可

The whole process was very relaxed and enjoyable.

Some key codes

Code, but also can not explain all, here only some of the more important steps to achieve, the rest can refer to the source code

UA conventions

The previous JS project has already mentioned the UA convention, that is, when loading for WebView, unify the following UA logo in WebView

//get default ua  nsstring *  Defaultua =  [[UIWebView new ] stringbyevaluatingjavascriptfromstring:  @ "navigator.useragent" ] NSString *  version =  @ "1.0.0"          NSString *  Customerua =  [Defaultua Stringbyappendingstring:  [NSString stringwithformat:  @  version]] [[Nsuserdefaults Standarduserdefaults] Registerdefaults:  @{ @ "useragent"  :  customerua} ] 
Monitor the triggering of Jsbridge

When creating WebView, the QHBaseWebLoader Agent listener is created

    //Create WebView containerWkwebviewconfiguration*Webconfig=[[Wkwebviewconfiguration alloc] init];Wkusercontentcontroller*Usercontentvc=[[Wkusercontentcontroller alloc] init];    Webconfig.Usercontentcontroller =Usercontentvc;Wkwebview*Wk=[[Wkwebview alloc] initWithFrame:Cgrectzero Configuration:Webconfig];[ Self.View Addsubview:wk;     Self.WV =Wk;     Self.WV.navigationdelegate =Self;     Self.WV.uidelegate =Self;     Self.WV.translatesautoresizingmaskintoconstraints =NO;... Self.Bridge =[Wkwebviewjavascriptbridge Bridgeforwebview: Self.WV];[ Self.Bridge setwebviewdelegate:Self;[ Self.WV.Configuration.Usercontentcontroller Addscriptmessagehandler: Self.Bridge name:@"Wkwebviewjavascriptbridge"];

Then h5 in the following call:

window.webkit.messageHandlers.WKWebViewJavascriptBridge.postMessage(...);

Then WKWebViewJavascriptBridge internally, accept the message and parse it yourself

- WKScriptMessageHandler- (void)userContentController:*)userContentController didReceiveScriptMessage:*{    if ([message.nameisEqualToString:@"WKWebViewJavascriptBridge"{        excuteMessage:message.body];    }}
Other

The other thing about iOS is that unlike Android, many standard HTML5 content does not require additional compatibility (e.g. Fileinput file selection, etc.)

Other content, as mentioned in the Android implementation, here will not repeat, you can directly refer to the source code

In addition, follow-up operations such as container optimization will be organized separately and added to this series.

Front-end Page examples

For convenience, directly integrated into the res/ , the portal page by default will load it, you can directly see the source code

Return to the root directory
    • "Quickhybrid" How to implement a hybrid framework
Source

githubOn the implementation of this framework

Quickhybrid/quickhybrid

Project implementation of the "Quickhybrid" iOS side

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.