Weex项目初始化weex-iOS集成

项目初始化

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

Weex项目初始化weex-iOS集成

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

Weex项目初始化weex-iOS集成

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

Weex项目初始化weex-iOS集成

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

Weex项目初始化weex-iOS集成

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

Weex项目初始化weex-iOS集成

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

Weex项目初始化weex-iOS集成

现在项目目录变成了这样,以后点击xcworkspace文件打开项目

Weex项目初始化weex-iOS集成

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

Weex项目初始化weex-iOS集成

自动创建的文件:

Weex项目初始化weex-iOS集成

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

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

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

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

加载weex页面

xcode打开workspace项目文件

Weex项目初始化weex-iOS集成

打开AppDelegate.m添加一下内容

Weex项目初始化weex-iOS集成

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

Weex项目初始化weex-iOS集成

效果是这样的

Weex项目初始化weex-iOS集成

weex视图控制器的初始化

ViewController.h:

  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  


#import "ViewController.h"

#import <WeexSDK/WeexSDK.h>



@interface ViewController()

@property(nonatomic,strong)WXSDKInstance *instance;

@property(nonatomic,strong)UIView *weexView;

@end





@implementation ViewController


NSURL *jsUrl;




-(instancetype)initWithJS:(NSString *)filePath {


    self = [superinit];

    if (self) {

        NSString *mai = [[NSBundlemainBundle]bundlePath];


        //        NSString *path=[NSString stringWithFormat:@"http://192.168.232.13:8080/examples/js/%@",filePath];  

        

         NSString *path=[NSStringstringWithFormat:@"file://%@/js/%@",[NSBundlemainBundle].bundlePath,filePath];

        NSLog(@"-----path:%@",path);

        jsUrl=[NSURLURLWithString:path];

    }

    returnself;

    




}


- (void)viewDidLoad {

    [superviewDidLoad];

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


    _instance = [[WXSDKInstancealloc]init];

    _instance.viewController =self;

    _instance.frame=self.view.frame;

    __weaktypeof(self) weakSelf =self;

    _instance.onCreate = ^(UIView *view) {

        [weakSelf.weexViewremoveFromSuperview];

        weakSelf.weexView = view;

        [weakSelf.viewaddSubview:weakSelf.weexView];

    };

    _instance.onFailed = ^(NSError *error) {

        NSLog(@"加载错误%@",error);

    };

    

    _instance.renderFinish = ^ (UIView *view) {

        NSLog(@"加载完成");

    };

    if (!jsUrl) {

        return;

    }

    [_instancerenderWithURL:jsUrl];

    

    

    self.view.backgroundColor=[UIColorwhiteColor];

}


- (void)dealloc

{

    [_instancedestroyInstance];

}



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end



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

引入头文件

#import "ViewController.h"

创建导航视图:

#import "AppDelegate.h"

#import <WeexSDK/WeexSDK.h>

#import "ViewController.h"



@interface AppDelegate ()


@end


@implementation AppDelegate



- (void)initWeex {

    [WXAppConfigurationsetAppGroup:@"ailinggong"];

    [WXAppConfigurationsetAppName:@"weexDemo"];

    [WXAppConfigurationsetAppName:@"1.0.0"];

    [WXSDKEngineinitSDKEnviroment];


}




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

    // Override point for customization after application launch.

    [selfinitWeex];

    

    ViewController *vc=[[ViewControlleralloc]initWithJS:@"about-me.js"];

    UINavigationController *nav=[[UINavigationControlleralloc]initWithRootViewController:vc];

    self.window.rootViewController=nav;

    

    returnYES;

}




运行
Weex项目初始化weex-iOS集成


项目初始化

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

Weex项目初始化weex-iOS集成

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

Weex项目初始化weex-iOS集成

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

Weex项目初始化weex-iOS集成

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

Weex项目初始化weex-iOS集成

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

Weex项目初始化weex-iOS集成

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

Weex项目初始化weex-iOS集成

现在项目目录变成了这样,以后点击xcworkspace文件打开项目

Weex项目初始化weex-iOS集成

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

Weex项目初始化weex-iOS集成

自动创建的文件:

Weex项目初始化weex-iOS集成

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

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

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

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

加载weex页面

xcode打开workspace项目文件

Weex项目初始化weex-iOS集成

打开AppDelegate.m添加一下内容

Weex项目初始化weex-iOS集成

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

Weex项目初始化weex-iOS集成

效果是这样的

Weex项目初始化weex-iOS集成

weex视图控制器的初始化

ViewController.h:

  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  


#import "ViewController.h"

#import <WeexSDK/WeexSDK.h>



@interface ViewController()

@property(nonatomic,strong)WXSDKInstance *instance;

@property(nonatomic,strong)UIView *weexView;

@end





@implementation ViewController


NSURL *jsUrl;




-(instancetype)initWithJS:(NSString *)filePath {


    self = [superinit];

    if (self) {

        NSString *mai = [[NSBundlemainBundle]bundlePath];


        //        NSString *path=[NSString stringWithFormat:@"http://192.168.232.13:8080/examples/js/%@",filePath];  

        

         NSString *path=[NSStringstringWithFormat:@"file://%@/js/%@",[NSBundlemainBundle].bundlePath,filePath];

        NSLog(@"-----path:%@",path);

        jsUrl=[NSURLURLWithString:path];

    }

    returnself;

    




}


- (void)viewDidLoad {

    [superviewDidLoad];

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


    _instance = [[WXSDKInstancealloc]init];

    _instance.viewController =self;

    _instance.frame=self.view.frame;

    __weaktypeof(self) weakSelf =self;

    _instance.onCreate = ^(UIView *view) {

        [weakSelf.weexViewremoveFromSuperview];

        weakSelf.weexView = view;

        [weakSelf.viewaddSubview:weakSelf.weexView];

    };

    _instance.onFailed = ^(NSError *error) {

        NSLog(@"加载错误%@",error);

    };

    

    _instance.renderFinish = ^ (UIView *view) {

        NSLog(@"加载完成");

    };

    if (!jsUrl) {

        return;

    }

    [_instancerenderWithURL:jsUrl];

    

    

    self.view.backgroundColor=[UIColorwhiteColor];

}


- (void)dealloc

{

    [_instancedestroyInstance];

}



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end



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

引入头文件

#import "ViewController.h"

创建导航视图:

#import "AppDelegate.h"

#import <WeexSDK/WeexSDK.h>

#import "ViewController.h"



@interface AppDelegate ()


@end


@implementation AppDelegate



- (void)initWeex {

    [WXAppConfigurationsetAppGroup:@"ailinggong"];

    [WXAppConfigurationsetAppName:@"weexDemo"];

    [WXAppConfigurationsetAppName:@"1.0.0"];

    [WXSDKEngineinitSDKEnviroment];


}




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

    // Override point for customization after application launch.

    [selfinitWeex];

    

    ViewController *vc=[[ViewControlleralloc]initWithJS:@"about-me.js"];

    UINavigationController *nav=[[UINavigationControlleralloc]initWithRootViewController:vc];

    self.window.rootViewController=nav;

    

    returnYES;

}




运行
Weex项目初始化weex-iOS集成