IOS项目集成Weex
安裝homebrew
通過homebrew安裝nodejs
通過npm安裝weex-toolkit
npm install -g weex-toolkit
安裝cocoapod
通過xcode創(chuàng)建項目WeexDemo
在WeexDemo目錄下執(zhí)行pod init創(chuàng)建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
執(zhí)行命令pod install
創(chuàng)建文件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>
執(zhí)行命令
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
總結(jié)
以上是生活随笔為你收集整理的IOS项目集成Weex的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在macOS搭建React Native
- 下一篇: Android项目集成Weex