CodePush is a cloud service that enables Cordova and React Native developers to deploy mobile app updates directly to their users’ devices. It works by acting as a central repository that developers can publish certain updates to (e.g. JS, HTML, CSS and image changes), and that apps can query for updates from (using our provided client SDKs). This allows you to have a more deterministic and direct engagement model with your end-users, while addressing bugs and/or adding small features that don’t require you to re-build a binary and/or re-distribute it through any public app stores.html
CodePush支持多平台,二者热部署方式略有差别,具体去官网查看,本人暂时只在iOS上开发node
安装客户端react
npm install -g code-push-cli
建立帐户(我使用的GitHub的帐号)ios
code-push register
将本身的App项目加入服务git
code-push app add <MyAppName>
使用以下命令在项目的更目录下执行(相似给项目加一个ReacNatice plugin)github
npm install --save react-native-code-push
打开项目AwesomeProject,在项目根目录下会有文件夹react-native-code-pushnpm
将react-native-code-push文件夹中CodePush.xcodeproj
直接拉入项目中Libraries中swift
将Libraries/CodePush.xcodeproj/Products
中的libCodePush.a
直接拖入 Link Binary With Libraries中react-native
点击Link Binary With Libraries的加号,选择libz.tbd
加入xcode
在Build Settings的Header Search Paths那一项中加入$(SRCROOT)/../node_modules/react-native-code-push
在Xcode打开项目在AppDelegate.m
中引入
#import "CodePush.h"
在AppDelegate
中找到 加载发布版本中的JS Bundle文件的那段代码
jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
将这段话替换为:
jsCodeLocation = [CodePush bundleURL];
该文章是讨论在Release下进行相关的操做,若是是debug版本加上以下代码,系统在运行时候会自动切换。
NSURL *jsCodeLocation; #ifdef DEBUG jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"]; #else jsCodeLocation = [CodePush bundleURL]; #endif
在命令行下使用code-push deployment ls <AppName> --displayKeys
查出Staging
的值,在info.plist文件中添加CodePushDeploymentKey
而且把Staging
的值填入。
在Info.plist
中将Bundle versions string, short
的值修改成1.0.0
在js文件中引入(该项目文件是index.ios.js)
import codePush from "react-native-code-push";
在componentDidMount
调用sync
方法,当你的App启动的时候会在后台更新
componentDidMount(){ codePush.sync(); }
当你修改js文件的时候,而且想马上发布。则仅仅须要如下的操做:
将你修改的js文件(当前文件是index.ios.js)打包为二进制文件,放入指定的地方(当前位置为根目录)。
react-native bundle --platform ios --entry-file index.ios.js --bundle-output codepush.js --dev false
将二进制文件push到staging 环境中
code-push release <appName> <updateContents> <targetBinaryVersion> [--deploymentName <deploymentName>] [--description <description>] [--mandatory] code-push release AwesomeProject2 codepush.js 1.0.0
–assets-dest 就是你放图片的位置(当前仅仅是作测试,实际上最好建个文件夹专门存入相关图片)
react-native bundle --platform ios --entry-file index.ios.js --bundle-output ./main.jsbundle --assets-dest ./
将二进制文件push到staging 环境中
code-push release AwesomeProject2 codepush.js 1.0.0
而后在你手机上退出App 再次进入就会发现界面改变啦!!
其余热更新思路:
https://github.com/fengjundev/React-Native-Remote-Update#使用react-native实现app热部署的一次实践