IOS项目集成Weex

来源:互联网 发布:网络攻防大赛 编辑:程序博客网 时间:2024/06/06 02:51

安装homebrew


通过homebrew安装nodejs


通过npm安装weex-toolkit

npm install -g weex-toolkit


安装cocoapod


通过xcode创建项目WeexDemo


在WeexDemo目录下执行pod init创建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


执行命令pod install


创建文件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>


执行命令

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

    [WXAppConfigurationsetAppGroup:@"AliApp"];

    [WXAppConfigurationsetAppName:@"WeexDemo"];

    [WXAppConfigurationsetAppVersion:@"1.0.0"];

    

    //init sdk enviroment

    [WXSDKEngineinitSDKEnviroment];

    

    //register custom module and componentoptional

    //[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

    [WXLogsetLogLevel: WXLogLevelAll];

    

    returnYES;

}


编辑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 {

    [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.view = view;

        

        [weakSelf.viewaddSubview:weakSelf.weexView];

    };

    

    _instance.onFailed = ^(NSError *error) {

        //process failure

    };

    

    _instance.renderFinish = ^ (UIView *view) {

        //process renderFinish

    };

    

    self.url = [[NSBundlemainBundle] URLForResource:@"hello"withExtension:@"js"];

    [_instancerenderWithURL:self.url];

}


- (void)dealloc {

    [_instancedestroyInstance];

}


运行程序



源代码工程

https://github.com/chenhaifeng2016/WeexDemoIOS


0 0