Weex项目初始化weex-iOS集成

来源:互联网 发布:淘宝商城购买cf装备 编辑:程序博客网 时间:2024/05/12 08:42

项目初始化

1、没有现成的工程的话新建iOS项目


命令行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 src -o js生成最终需要的js文件

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

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

加载weex页面

xcode打开workspace项目文件


打开AppDelegate.m添加一下内容


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


效果是这样的


weex视图控制器的初始化

ViewController.h:

[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  ViewController.h  
  3. //  weexDemo3  
  4. //  
  5. //  Created by admin on 16/8/3.  
  6. //  Copyright © 2016年 admin. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface ViewController : UIViewController  
  12. - (instancetype)initWithJs:(NSString *)filePath;  
  13. @end  

ViewController.m:

[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  ViewController.m  
  3. //  weexDemo3  
  4. //  
  5. //  Created by admin on 16/8/3.  
  6. //  Copyright © 2016年 admin. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10. #import <WeexSDK/WXSDKInstance.h>  
  11.   
  12. @interface ViewController ()  
  13. @property (nonatomicstrongWXSDKInstance *instance;  
  14. @property (nonatomicstrongUIView *weexView;  
  15. @end  
  16.   
  17. @implementation ViewController{  
  18.     NSURL *jsUrl;  
  19. }  
  20.   
  21. - (instancetype)initWithJs:(NSString *)filePath  
  22. {  
  23.     self = [super init];  
  24.     if (self) {  
  25.         //远程js文件  
  26. //        NSString *path=[NSString stringWithFormat:@"http://192.168.232.13:8080/examples/js/%@",filePath];  
  27.         //本地js文件  
  28.                 NSString *path=[NSString stringWithFormat:@"file://%@/js/%@",[NSBundle mainBundle].bundlePath,filePath];  
  29.         NSLog(@"-----path:%@",path);  
  30.         jsUrl=[NSURL URLWithString:path];  
  31.     }  
  32.     return self;  
  33. }  
  34.   
  35. - (void)viewDidLoad {  
  36.     [super viewDidLoad];  
  37.     _instance = [[WXSDKInstance alloc] init];  
  38.     _instance.viewController = self;  
  39.     _instance.frame=self.view.frame;  
  40.     __weak typeof(self) weakSelf = self;  
  41.     _instance.onCreate = ^(UIView *view) {  
  42.         [weakSelf.weexView removeFromSuperview];  
  43.         weakSelf.weexView = view;  
  44.         [weakSelf.view addSubview:weakSelf.weexView];  
  45.     };  
  46.     _instance.onFailed = ^(NSError *error) {  
  47.         NSLog(@"加载错误");  
  48.     };  
  49.       
  50.     _instance.renderFinish = ^ (UIView *view) {  
  51.         NSLog(@"加载完成");  
  52.     };  
  53.     if (!jsUrl) {  
  54.         return;  
  55.     }  
  56.     [_instance renderWithURL: jsUrl];  
  57.     self.view.backgroundColor=[UIColor whiteColor];  
  58. }  
  59.   
  60. - (void)didReceiveMemoryWarning {  
  61.     [super didReceiveMemoryWarning];  
  62.     // Dispose of any resources that can be recreated.  
  63. }  
  64.   
  65. - (void)dealloc  
  66. {  
  67.     [_instance destroyInstance];  
  68. }  
  69.   
  70. @end  


再打开AppDelegate.m创建导航控制器

引入头文件

#import "ViewController.h"

创建导航视图:

[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  2.     //weex  
  3.     [self initWeex];  
  4.     ViewController *vc=[[ViewController alloc]initWithJs:@"main.js"];  
  5.     UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:vc];  
  6.     self.window.rootViewController=nav;  
  7.     return YES;  
  8. }  

运行

图片不显示是因为图片加载需要自己创建模块,可以直接把demo的代码和pod配置粘过来使用

0 0
原创粉丝点击