iOS集成weex(0)

新建項目

命令行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運行,至此集成結束,下一節總結開發基礎

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章