Introduction Document: Http://weex.apache.org/cn/guide/integrate-to-your-app.html
Official website cocoapods/Manual Integration document:https://open.taobao.com/doc2/detail?spm=a219a.7629140.0.0.tfddsv&&doctype=1& articleid=104829
Note: There are any differences, please follow the official website, in this deep thanks to Ali programmer Daniel Painstakingly, do open source work, pay tribute to.
First, Weex manual integration into the native application
GitHub Official Source:
Https://github.com/alibaba/weex
Https://github.com/alibaba/weex/archive/dev.zip
In the XCode open weex/ios/sdk directory under theWeexSDK.xcodeproj
such as switching target
Compile the project, or use XCode the shortcut keys directly? + b
Finally you can see weex/ios/sdk/Products the packages generated under WeexSDK.framework the directory
You can modify the WEEXSDK and then package to build your own custom Weex SDK
Import SDK to Project
and import the libraries of the system, such as
Add Socketrocket dependency: Copy here SRWebSocket.h/m to your own project (if your project uses cocoapods, add pod ‘SocketRocket‘ to Podfie)
Add Main.js (in Weexsdk.framework package)
Important Notes
Add -ObjC as (note case)
========= to this integration complete the rest is that the code is moving around =========
Environment initialization
Weex the initialization of the operating environment, mainly includes the following aspects: application configuration (application name, version number, UA, etc.), registering custom module and component, registering the SDK already has the implementation class of the Protocol. All the initialization steps are completed in Appdelegate.
1. header file
#import <WeexSDK/WXSDKEngine.h> #import <WeexSDK/WXLog.h> #import <WeexSDK/WXDebugTool.h> #import <WeexSDK/WXAppConfiguration.h>
Most of the interfaces opened by WXSDKENGINE:SDK are declared here.
Wxlog: Controls the level of log output, including verbose, Debug, Info, Warning, Error, and developers can set output levels on demand.
Wxdebugtool:weex provides external debugging tools.
Wxappconfiguration: A business configuration developed using Weex.
2. Initialization
Executed in the didfinishlaunchingwithoptions callback of the APPDELEGATE.M.
Business configuration, not required [wxappconfiguration setappgroup:@ "Aliapp"]; [Wxappconfiguration setappname:@ "Weexdemo"]; [Wxappconfiguration setappversion:@ "1.0.0"]; Initializing the SDK environment [Wxsdkengine initsdkenviroment]; Registering custom module and component, not required [wxsdkengine registercomponent:@ "MyView" Withclass:[myviewcomponent class]]; [Wxsdkengine registermodule:@ "event" Withclass:[wxeventmodule class]]; The implementation class of the registration agreement, not required [Wxsdkengine Registerhandler:[wxnavigationdefaultimpl new] Withprotocol: @protocol ( Wxnavigationprotocol)]; Set log output Level: Debug environment Default to debug, the official release will automatically shut down. [Wxlog Setloglevel:wxloglevelverbose];
After the above simple configuration, WEEXSDK environment initialization has been completed, the following into the Weex dynamic page rendering link.
Weex Page Rendering
Weex supports both full-page and page-local two different rendering modes. Using the method in iOS is simple, just add the Weex rendered view to the parent container. Here's how to render Weex pages in the most common iOS container viewcontroller.
#import <WeexSDK/WXSDKInstance.h>
Now let's introduce what the next wxsdkinstance really is and how to use it.
Wxsdkinstance, simply put, is the instance object that the Weex renders. It provides developers with many interfaces related to page rendering, including Renderwithurl, Refreshinstance, and Destroyinstance, and provides several important callback interfaces that allow developers to handle their logic according to different business scenarios. For example, onfailed also provides performance monitoring-related interfaces.
2. Page rendering
1. header file
-(void) viewdidload { [super viewdidload]; _instance = [[Wxsdkinstance alloc] init]; _instance.viewcontroller = self; _instance.frame = Self.view.frame; Required [_instance renderWithURL:self.url options:@{@ "Bundleurl": [Self.url absolutestring]} Data:nil]; __weak typeof (self) weakself = self; _instance.oncreate = ^ (UIView *view) { [Weakself.weexview Removefromsuperview]; [Weakself.view AddSubview:weakSelf.weexView]; }; _instance.onfailed = ^ (Nserror *error) { //Handle the logic of the failed callback. }; _instance.renderfinish = ^ (UIView *view) { //Handle the logic of page rendering completion. };}
The above is a simple example of rendering a page with a Weex instance. A few of them need to be highlighted below:
Instance.viewcontroller: In general, iOS is based on different viewcontroller, and many of the underlying operations need to know what Viewcontroller objects are currently in place. It is therefore necessary to inform instance of the current Viewcontroller.
Instance.frame: According to Weex's design specification for instance, the position and dimensions of the outermost body are required to be known in the rendering. The setting of this frame value is consistent with the view.frame that eventually gets in the callback.
Of course, in many scenarios, you only need to render Weex chunks locally on a native page. Very simply, you only need to set the Instance.frame to the location size of the target chunk.
Renderwithurl: The usual way of rendering: one, directly enter the URL (can be file://or HTTP (s)://), and the other, sourcecode, that is, JavaScript source code. The options parameter, which indicates that the developer can pass WEEXSDK parameters to the front end, such as Bundleurl. Data parameter, which represents the page data injected into the template of the Weex, is typically derived from the data request of native and, of course, can be injected after the request is completed in the front-end logic.
_instance.oncreate:weex the callback after the outermost body rendering of the page is complete. In this callback, the Weex rendered Rootview has been determined and can be output and added to the parent container.
_instance.renderfinish: Unlike OnCreate, renderfinish means that all the Weex page elements have been rendered and the entire rendering process is finished.
For other interfaces that are closely related to instance, see detailed instructions for WXSDKInstance.h.
3. Page Destruction
Remember to destroy the Viewcontroller while the Weex instance is destroyed, or there will be a memory leak.
-(void) dealloc{ [_instance destroyinstance];}
IOS Hybrid Development--weex