iOS集成weex(0)

来源:互联网 发布:羽毛球计分软件 编辑:程序博客网 时间:2024/05/23 20:03

新建项目

命令行cd到项目根目录 执行 pod init,会创建一个pod配置文件

这里写图片描述

用编辑器打开,加上 pod ‘WeexSDK’, :path=>’./sdk/’

这里写图片描述

下载最新的weexSDK https://github.com/alibaba/weex

在ios目录下有个sdk文件夹,把它复制到ios项目根目录,和podFile里配置的路径一致

这里写图片描述

关掉xcode,在当前目录,命令行执行pod install,

这里写图片描述
现在项目目录变成了这样,以后点击xcworkspace文件打开项目

这里写图片描述

创建一个新目录weex,命令行cd到weex目录,执行weex init,会提示你输入项目名称

这里写图片描述

在当前目录命令行执行npm install,安装依赖库

创建一个文件夹js,命令行执行weex compile src js -w生成最终需要的js文件

也可以weex src/main.we在浏览器预览

或者weex src/main.we –qr 生成二维码,用playground App 扫描预览

加载weex页面

xcode打开workspace项目文件

这里写图片描述

打开AppDelegate.m添加一下内容

这里写图片描述

将之前创建的js文件夹拖到xcode工程的文件列表

这里写图片描述

weex视图控制器的初始化

ViewController.h:

#import <UIKit/UIKit.h>@interface ViewController : UIViewController- (instancetype)initWithJs:(NSString *)filePath;@end

ViewController.m:

#import "ViewController.h"#import <WeexSDK/WXSDKInstance.h>@interface ViewController ()@property (nonatomic, strong) WXSDKInstance *instance;@property (nonatomic, strong) UIView *weexView;@property (nonatomic, strong) NSURL *jsUrl;@end@implementation ViewController- (instancetype)initWithJs:(NSString *)filePath {    self = [super init];    if(self){        //远程js文件        //        NSString *path=[NSString stringWithFormat:@"http://192.168.232.13:8080/examples/js/%@",filePath];        //本地js文件        NSString *path=[NSString stringWithFormat:@"file://%@/js/%@",[NSBundle mainBundle].bundlePath,filePath];        NSLog(@"-----path:%@",path);        self.jsUrl=[NSURL URLWithString:path];    }    return self;}- (void)viewDidLoad {    [super viewDidLoad];    self.instance = [[WXSDKInstance alloc] init];    self.instance.viewController = self;    self.instance.frame=self.view.frame;    __weak typeof(self) weakSelf = self;    self.instance.onCreate = ^(UIView *view) {        [weakSelf.weexView removeFromSuperview];        weakSelf.weexView = view;        [weakSelf.view addSubview:weakSelf.weexView];    };    self.instance.onFailed = ^(NSError *error) {        NSLog(@"加载错误");    };    self.instance.renderFinish = ^ (UIView *view) {        NSLog(@"加载完成");    };    if (!self.jsUrl) {        return;    }    [self.instance renderWithURL: self.jsUrl];    self.view.backgroundColor=[UIColor whiteColor];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (void)dealloc {    [self.instance destroyInstance];}@end

创建视图:

#import "AppDelegate.h"#import <WeexSDK/WeexSDK.h>#import "ViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (void)initWeex {    //business configuration    [WXAppConfiguration setAppGroup:@"AliApp"];    [WXAppConfiguration setAppName:@"WeexDemo"];    [WXAppConfiguration setAppVersion:@"1.0.0"];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    [self initWeex];    ViewController *vc  = [[ViewController alloc] initWithJs:@"foo.js"];    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vc];    self.window.rootViewController = navi;    return YES;}- (void)applicationWillResignActive:(UIApplication *)application {    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application {    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application {    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application {    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application {    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end

xcode运行,至此集成结束,下一节总结开发基础

原创粉丝点击