IOS项目集成Weex

安装homebrew


通过homebrew安装nodejs


通过npm安装weex-toolkit

npm install -g weex-toolkit


安装cocoapod


通过xcode创建项目WeexDemo


在WeexDemo目录下执行pod init创建Podfile文件


编辑Podfile

# Uncomment the next line to define a global platform for your project

 platform :ios, '9.0'


target 'WeexDemo' do

  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks

  # use_frameworks!


  # Pods for WeexDemo

  pod 'WeexSDK', '0.9.5'


end


执行命令pod install


创建文件hello.we

<template>
  <div class="wrapper">
    <text class="weex">Hello Weex !</text>
    <text class="vue">Hello Vue !</text>
  </div>
</template>
<style scoped>
  .wrapper {
    flex-direction: column;
    justify-content: center;
  }
  .weex {
   font-size: 60px;
   text-align: center;
   color: #1B90F7;
  }
  .vue {
   font-size: 60px;
   text-align: center;
   margin-top: 30px;
   color: #41B883;
  }
</style>


执行命令

weex hello.we -o hello.js

把生成的hello.js加入项目工程


编辑AppDelegate.m文件

#import <WeexSDK/WeexSDK.h>


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    //business configuration

    [WXAppConfiguration setAppGroup:@"AliApp"];

    [WXAppConfiguration setAppName:@"WeexDemo"];

    [WXAppConfiguration setAppVersion:@"1.0.0"];

    

    //init sdk enviroment

    [WXSDKEngine initSDKEnviroment];

    

    //register custom module and componentoptional

    //[WXSDKEngine registerComponent:@"MyView" withClass:[MyViewComponent class]];

    //[WXSDKEngine registerModule:@"event" withClass:[WXEventModule class]];

    

    //register the implementation of protocol, optional

    //[WXSDKEngine registerHandler:[WXNavigationDefaultImpl new] withProtocol:@protocol(WXNavigationProtocol)];

    

    //set the log level

    [WXLog setLogLevel: WXLogLevelAll];

    

    return YES;

}


编辑ViewController.h文件

#import <UIKit/UIKit.h>

#import <WeexSDK/WXSDKInstance.h>


@interface ViewController : UIViewController


@property (nonatomic, strong) WXSDKInstance * instance;

@property (nonatomic, strong) UIView * weexView;

@property (nonatomic, strong) NSURL * url;


@end


编辑ViewController.m文件

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    _instance = [[WXSDKInstance alloc] init];

    _instance.viewController = self;

    _instance.frame = self.view.frame;

    

    __weak typeof(self) weakSelf = self;

    

    _instance.onCreate = ^(UIView *view) {

        [weakSelf.weexView removeFromSuperview];

        

        weakSelf.view = view;

        

        [weakSelf.view addSubview:weakSelf.weexView];

    };

    

    _instance.onFailed = ^(NSError *error) {

        //process failure

    };

    

    _instance.renderFinish = ^ (UIView *view) {

        //process renderFinish

    };

    

    self.url = [[NSBundle mainBundle] URLForResource:@"hello" withExtension:@"js"];

    [_instance renderWithURL:self.url];

}


- (void)dealloc {

    [_instance destroyInstance];

}


运行程序



源代码工程

https://github.com/chenhaifeng2016/WeexDemoIOS


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值