CodePush

安装

npm install -g code-push-cli

注册

code-push register

弹出一个网页,使用MS/GitHub帐号登录,返回一个AccessKey,记下来粘贴到Bash中

添加应用

code-push app add ReactNews

返回两个部署,分别为Production和Staging,由于网络不好,可以把DeploymentKey记下来

项目设置

npm install react-native-code-push@latest --save

老版本的RN可以执行

rnpm link react-native-code-push

新版本RN可以直接用react-native的命令

react-native link react-native-code-push

系统会问你为Android设置的DeploymentKey

iOS的需要手动配置,在Info.plist文件中添加CodePushDeploymentKey键

代码配置

iOS项目中AppDelegate:

import "CodePush.h"

然后修改代码:

#ifdef DEBUG
  jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];
#else
  jsCodeLocation = [CodePush bundleURL];

在index.ios.js的componentDidMount中写入:

import codePush from "react-native-code-push";

然后两种方式启动:

componentDidMount() {
  codePush.sync();
}

或者在根组件外,写:

class MyApp extends Component {
  ......
}
......
MyApp = codePush(MyApp);

打包

react-native bundle --platform ios --entry-file index.ios.js --bundle-output ./release/main.jsbundle --assets-dest ./release --dev false

iOS平台 index.ios.js入口文件 包输出目录 assets输出位置 非dev模式

发布包

通用命令

code-push release ReactNews folderName 1.0 [-d Staging/Production]

RN专用命令

code-push release-react ReactNews ios

RN专用发布命令,自动打包,自动匹配target的version,默认发布到Staging部署

查看

code-push deployment ls ReactNews [-k]

查看已发布的包,-k参数可选,默认不显示DeploymentKey