加载中...

1.4.4 集成到iOS


cocoaPods 引入 Weex iOS SDK到工程

可以通过源码编译出Weex SDK,可以在新的feature 或者bugfix 分支,尝试最新的feature

  1. 从github上clone一份代码

    1. git clone https://github.com/alibaba/weex.git
  2. 把WeexSDK 导入到你已有的项目,如果没有,可以参考新建项目
    拷贝 ios/sdk下面目录到你的项目目录,在添加依赖之前,确保项目目录有Podfile,如果没有,创建一个,用文本编辑器打开,添加如下依赖

如果使用正式版本如 0.6.1 的,就不需要做 拷贝ios/sdk 这个操作,直接引用cocoaPods的master repo 上就可以,这个需要在Podfile 最前面添加

  1. source 'https://github.com/CocoaPods/Specs.git'
  1. target 'YourTarget' do
  2. platform :ios, '7.0'
  3. pod 'WeexSDK', :path=>'./sdk/' # pod 'WeexSDK', '0.6.1'
  4. end

在命令行(terminal)中,切换到当前目录,运行 pod install,过一会,项目的 .workspace结尾的文件就被创建出来,到这步,依赖已经添加完了

  1. 初始化 Weex 环境
    在AppDelegate.m 文件中做初始化操作,一般会在 didFinishLaunchingWithOptions方法中如下添加

    1. //business configuration
    2. [WXAppConfiguration setAppGroup:@"AliApp"];
    3. [WXAppConfiguration setAppName:@"WeexDemo"];
    4. [WXAppConfiguration setAppVersion:@"1.0.0"];
    5. //init sdk enviroment
    6. [WXSDKEngine initSDKEnviroment];
    7. //register custom module and component,optional
    8. [WXSDKEngine registerComponent:@"MyView" withClass:[MyViewComponent class]];
    9. [WXSDKEngine registerModule:@"event" withClass:[WXEventModule class]];
    10. //register the implementation of protocol, optional
    11. [WXSDKEngine registerHandler:[WXNavigationDefaultImpl new] withProtocol:@protocol(WXNavigationProtocol)];
    12. //set the log level
    13. [WXLog setLogLevel: WXLogLevelAll];
  2. 渲染 weex Instance
    Weex 支持整体页面渲染和部分渲染两种模式,你需要做的事情是用指定的URL渲染weex的view,然后添加到它的父容器上,父容器一般都是viewController

  1. #import <WeexSDK/WXSDKInstance.h>
  2. - (void)viewDidLoad
  3. {
  4. [super viewDidLoad];
  5. _instance = [[WXSDKInstance alloc] init];
  6. _instance.viewController = self;
  7. _instance.frame = self.view.frame;
  8. __weak typeof(self) weakSelf = self;
  9. _instance.onCreate = ^(UIView *view) {
  10. [weakSelf.weexView removeFromSuperview];
  11. [weakSelf.view addSubview:weakSelf.weexView];
  12. };
  13. _instance.onFailed = ^(NSError *error) {
  14. //process failure
  15. };
  16. _instance.renderFinish = ^ (UIView *view) {
  17. //process renderFinish
  18. };
  19. [_instance renderWithURL:self.url options:@{@"bundleUrl":[self.url absoluteString]} data:nil];
  20. }

WXSDKInstance 是很重要的一个类,提供了基础的方法和一些回调,如renderWithURL,onCreate,onFailed等,可以参见 WXSDKInstance.h的 声明
3. 销毁Weex Instance
在 viewController的 dealloc 阶段 销毁掉weex instance, 释放内存,避免造成内存泄露

  1. - (void)dealloc
  2. {
  3. [_instance destroyInstance];
  4. }

导入 Weex SDK framework到工程

参考此处直接导入weexSDK


还没有评论.