Code Push是微软提供的一套能够热更新React Native的服务。能够使用微软的服务器也能够本身部署服务器。react
1.安装Code Pushnpm
使用命令npm install -g code-push-cli安装Code Push。react-native
2.注册Code Push帐号服务器
在终端输入code-push register就会跳转到受权网页,受权完成后,CodePush会显示你的Access Key,复制输入到终端便可完成注册并登录。app
(若是不输入code-push logout退出登陆命令,登陆状态会一直有效)spa
3.在终端输入code-push app add <appName> <os> <platform>便可完成建立项目。code
4.在要集成的项目输入npm install --save react-native-code-push下载code-push到项目,而后分别集成到IOS、Android,项目中填写建立项目的Key。component
5.在React Native根组件componentDidMount中添加更新代码orm
codePush.checkForUpdate(deploymentKey).then((update) => { if (!update) { Alert.alert("提示", "已经是最新版本--", [ { text: "Ok", onPress: () => { console.log("点了OK"); } } ]); } else { codePush.sync({ deploymentKey: deploymentKey, updateDialog: { optionalIgnoreButtonLabel: '稍后', optionalInstallButtonLabel: '当即更新', optionalUpdateMessage: '有新版本了,是否更新?', title: '更新提示' }, installMode: codePush.InstallMode.IMMEDIATE, }, (status) => { switch (status) { case codePush.SyncStatus.DOWNLOADING_PACKAGE: console.log("DOWNLOADING_PACKAGE"); break; case codePush.SyncStatus.INSTALLING_UPDATE: console.log(" INSTALLING_UPDATE"); break; } }, (progress) => { console.log(progress.receivedBytes + " of " + progress.totalBytes + " received."); } ); } }
6.输入命令code-push release-react <appName> <platform> [options]便可发布更新版本。blog
参数包括更新面向的版本号、是否强制更新、发布到Staging或Production等。