安装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 component,optional
//[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