article icon indicating copy to clipboard operation
article copied to clipboard

weex SDK 集成到工程 (integrate to ios)

Open acton393 opened this issue 8 years ago • 65 comments

本文档已迁移至 https://weex-project.io/cn/guide/integrate-to-your-app.html , 此处不再维护,谢谢。

通过cocoaPods 集成 Weex iOS SDK到你的项目

  1. 假设你已经完成了安装iOS 开发环境CocoaPods

  2. 导入Weex iOS SDK 到你已有的项目, 如果没有,可以参考新建项目 在继续下面内容之前,确保你已有的项目目录有名称为Podfile文件,如果没有,创建一个,用文本编辑器打开

    • 集成framework
      WeexSDK 在cocoaPods 上最新版本 可以在获取
      Podfile文件中添加如下内容

      ```
        source '[email protected]:CocoaPods/Specs.git' 
        #或者使用 source 'https://github.com/CocoaPods/Specs.git'
           target 'YourTarget' do
               platform :ios, '7.0' 
               pod 'WeexSDK', '0.9.5'   ## 建议使用WeexSDK新版本 
           end
        ```
      
    • 源码集成

      • 首先 拷贝ios/sdk目录到你已有项目目录(此处以拷贝到你已有项目的根目录为例子)
      • Podfile文件中添加
       source '[email protected]:CocoaPods/Specs.git' 
       # 或者使用 source 'https://github.com/CocoaPods/Specs.git'
         target 'YourTarget' do
             platform :ios, '7.0' 
             pod 'WeexSDK', :path=>'./sdk/' 
         end
      
  3. 打开命令行,切换到你已有项目Podfile这个文件存在的目录,执行 pod install,没有出现任何错误就已经完成环境配置

  4. 初始化 Weex 环境
    在AppDelegate.m 文件中做初始化操作,一般会在 didFinishLaunchingWithOptions方法中如下添加

    //business configuration
    [WXAppConfiguration setAppGroup:@"AliApp"];
    [WXAppConfiguration setAppName:@"WeexDemo"];
    [WXAppConfiguration setAppVersion:@"1.0.0"];
    
    //init sdk enviroment   
    [WXSDKEngine initSDKEnviroment];
    
    //register custom module and component,optional
    [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    
    [WXLog setLogLevel: WXLogLevelAll];
    
  5. 渲染 weex Instance Weex 支持整体页面渲染和部分渲染两种模式,你需要做的事情是用指定的URL渲染weex的view,然后添加到它的父容器上,父容器一般都是viewController

#import <WeexSDK/WXSDKInstance.h>
- (void)viewDidLoad 
{
    [super viewDidLoad];

    _instance = [[WXSDKInstance alloc] init];
    _instance.viewController = self;
    _instance.frame = self.view.frame; 

    __weak typeof(self) weakSelf = self;
    _instance.onCreate = ^(UIView *view) {
        [weakSelf.weexView removeFromSuperview];
        [weakSelf.view addSubview:weakSelf.weexView];
    };

    _instance.onFailed = ^(NSError *error) {
        //process failure
    };

    _instance.renderFinish = ^ (UIView *view) {
        //process renderFinish
    };
    [_instance renderWithURL:self.url options:@{@"bundleUrl":[self.url absoluteString]} data:nil];
}

WXSDKInstance 是很重要的一个类,提供了基础的方法和一些回调,如 renderWithURL,onCreate,onFailed等,可以参见 WXSDKInstance.h的声明 3. 销毁Weex Instance 在 viewController的 dealloc 阶段 销毁掉weex instance, 释放内存,避免造成内存泄露

- (void)dealloc
{
    [_instance destroyInstance];
}

导入 Weex SDK framework到工程

可以通过源码编译出Weex SDK,可以在新的feature 或者bugfix 分支,尝试最新的feature

参考此处直接导入weexSDK

acton393 avatar Jun 16 '16 15:06 acton393

'git clone https://github.com/alibaba/weex.git' 仓库不存在呀。

SeptemberMaples avatar Jun 18 '16 18:06 SeptemberMaples

有啥提示么

acton393 avatar Jun 19 '16 06:06 acton393

remote: Repository not found. 直接提示找不到啊。 你可以?

SeptemberMaples avatar Jun 19 '16 10:06 SeptemberMaples

@SeptemberMaples 你申请内测了吗?没有申请内测暂时是没有私有仓库访问权限的 可以先去这里 https://github.com/alibaba/weex 看有没有权限 详细: https://github.com/weexteam/article/issues/1

mrzzcn avatar Jun 19 '16 14:06 mrzzcn

@SeptemberMaples , 如 @mrzzcn 所提到的, 你先看下你有没有权限(直接访问 https://github.com/alibaba/weex 验证), https 的git 一般都可以clone 到的,

acton393 avatar Jun 20 '16 02:06 acton393

@acton393 我错了, 没有权限。

SeptemberMaples avatar Jun 20 '16 02:06 SeptemberMaples

嗯, 因为暂时还是内测阶段,所以只针对受邀请的开发者,需要权限的话,可以在这: http://alibaba.github.io/weex/ 提一下申请, 建议可以加一下qq群: 455207162

acton393 avatar Jun 20 '16 02:06 acton393

@acton393 谢谢。 已申请,已加。

SeptemberMaples avatar Jun 20 '16 02:06 SeptemberMaples

Podfile那块好像直接那么写会报错,要添加

target 'project-name' do
  pod 'WeexSDK', :path=>'./sdk/'
end

但是好像pod install成功之后,接着在didFinishLaunchingWithOptions里面添加方法还是找不到。不知道是不是我哪里出问题了。/sdk是放在项目根目录下的。

nicefe avatar Jun 20 '16 09:06 nicefe

@nicefe 嗯,是的, 在cocoaPods 比较新的版本需要添加

target 'your target-name' do    
        platform :ios, '7.0'    
        pod 'WeexSDK', :path=>'./sdk/'
end

pod install成功之后,打开*.xcworkspace didFinishLaunchingWithOptions 是在appDelegate 里面的,xcode新建工程时候会创建,

acton393 avatar Jun 20 '16 12:06 acton393

@nicefe pull request 文档已经更新~

acton393 avatar Jun 21 '16 03:06 acton393

@acton393 是不是要在AppDelegate.m文件头上添加

#import <WeexSDK/WXSDKEngine.h>
#import <WeexSDK/WXLog.h>
#import <WeexSDK/WXDebugTool.h>
#import <WeexSDK/WXAppConfiguration.h>

导入相关的库。才能用吧

nicefe avatar Jun 21 '16 14:06 nicefe

@nicefe 是的,你通过源码,参见当前ios集成文档 或者.framework 的方式引入 WeexSDK ,可以

framework可以自己打包也可以直接下载,参考此处readme

就可以用了

acton393 avatar Jun 22 '16 06:06 acton393

直接打开weex官方ios项目,Xcode编译报错,找不到各种库。泪喷。我觉得有必要把pod的事情说清楚,pod install总提示错误:[!] No podspec found for WeexSDK in ./sdk/,不知道为什么weex项目中ios目录下的sdk不放入playground目录,我复制一份sdk到项目/playgournd根目录,然后Podfile改为如下,点开WeexDemo.xcworkspace,编译的时候又提示缺少两张图片,我加了同名的png图片,然后可以顺利编译通过: source 'https://github.com/CocoaPods/Specs.git' target 'WeexDemo' do platform :ios, '7.0' pod 'SDWebImage', '3.7.5' pod 'SocketRocket', '0.4.2' pod 'ATSDK-Weex', '0.0.1' pod 'WeexSDK', :path=>'./sdk/'
end

duqian291902259 avatar Jun 24 '16 10:06 duqian291902259

@duqian291902259

  • 关于pod管理第三方库,README.md中的iOS操作步骤有说明。

https://github.com/alibaba/weex#ios

  • 关于Podfile文件中WeexSDK路径
pod 'WeexSDK', :path=>'./sdk/'

其中的'./sdk/'指的是相对于Podfile文件的路径。

laznrbfe avatar Jun 24 '16 11:06 laznrbfe

hi,@duqian291902259 是这样的,其实这是两件事情的

  • Weex 官方给出了一个demo(playground),它的使用方法见readme , 只需要在weex/ios/playground目录执行pod install 就可以了

  • Weex 还给出了如何自己集成一个使用Weex SDK的工程, 也就是这个issue,en

    在这个集成文档中需要做的就是需要把 sdk 拷贝到你自己项目目录下的, 然后添加相应的podfile内容的,都在这个issue 里面

我估计你是在跑playground的时候却看了这个doc了, 这个doc 是给集成weex SDK到自己的项目的同学的

直接打开weex官方ios项目,Xcode编译报错,找不到各种库 这个是需要在 playground 目录下面执行pod install 的, 详见readme

acton393 avatar Jun 24 '16 15:06 acton393

嗯。没开发过ios的同学,捣鼓起来可能需要学习下旁枝末节的东西。。要不然一脸懵逼。

duqian291902259 avatar Jun 25 '16 08:06 duqian291902259

<Weex>[info]WXBridgeContext.m:200, CreateInstance Finish...0.004585 [; 2016-07-19 18:56:49.835 Weexforios[11598:375381] [fg255,0,0; <Weex>[error]WXSDKError.m:39, [undefined:2:26382] SyntaxError: Unexpected token '<' Function@[native code]

init createInstance createInstance [;

这个是什么问题?求大神指导

shuideya avatar Jul 19 '16 10:07 shuideya

hi @shuideya 看这个日志是你的.we 文件里面多字符不大对, 你是否直接把.we 后缀的文件给你的工程(集成了WeexSDK)渲染

acton393 avatar Jul 20 '16 09:07 acton393

@acton393 是直接把.we 后缀的文件给工程渲染了

shuideya avatar Jul 21 '16 06:07 shuideya

@shuideya 不要直接渲染 .we 后缀文件, .we 后缀文件是需要transform成为bundlejs之后才可以供 sdk 渲染的 可以使用weex 的toolkit 做transform, 具体的方式参考一下这个 issue

acton393 avatar Jul 21 '16 06:07 acton393

#import <WeexSDK/WXLog.h> 这个现在已经不包含 WXLogLevelVerbose 了? 根据现在的文档,初始化的那个步骤在 import 所有必要的内容之后由于 WXLogLevelVerbose 找不到报错

kfitfk avatar Aug 03 '16 11:08 kfitfk

hi @kfitfk 现在的 WXLogLevelAll 应该对应之前的 WXLogLevelVerbose

acton393 avatar Aug 04 '16 07:08 acton393

hi @Davidleeeeee swift 的集成参考 这里

acton393 avatar Aug 12 '16 09:08 acton393

@duqian291902259 原文中有很多东西没有提到。导致要走很多弯路。看这里https://github.com/dengjunwen/weexPageDemo,有demo详细说明

dengjunwen avatar Aug 12 '16 10:08 dengjunwen

已提 pr 修复 ===== update ==== hi, @Davidleeeeee 嗯我已经看到, 会尽快想解决方法,主要是因为Weex SDK 中使用了 C的方法, 这个部分主要是用在 WXStorageModule md5 部分

acton393 avatar Aug 15 '16 07:08 acton393

@dengjunwen 已经404

Security111588 avatar Oct 12 '16 09:10 Security111588

@wang111588 https://github.com/dengjunwen/weexPageDemo

dengjunwen avatar Oct 12 '16 09:10 dengjunwen

https://github.com/dengjunwen/weexPageDemo https://github.com/dengjunwen/weexPageDemo

在 2016年10月12日,下午5:13,wang111588 [email protected] 写道:

@dengjunwen https://github.com/dengjunwen 已经404

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/weexteam/article/issues/18#issuecomment-253160968, or mute the thread https://github.com/notifications/unsubscribe-auth/ANdca_euVXzbSXNT_at769LXfIbvhpRTks5qzKTVgaJpZM4I3hrY.

dengjunwen avatar Oct 12 '16 09:10 dengjunwen

CaimatoiMac:Weexios caiye0607$ pod install Analyzing dependencies Fetching podspec for WeexSDK from ./sdk/ [!] No podspec found for WeexSDK in ./sdk/ 这什么意思

wzj583585700 avatar Oct 20 '16 03:10 wzj583585700