哈哈可很,今天是大豬在簡書寫的第一篇文章,步入正題睛驳,此文使用當(dāng)前最新版本的
RN
與Code-Push
進(jìn)行演示,其中的參數(shù)不會過多進(jìn)行詳細(xì)解釋稿械,更多參數(shù)解釋可參考其它文章选泻,這里只保證APP能正常進(jìn)行熱更新操作,方便快速入門,跟著大豬一起來快活吧页眯。
操作指南
以下操作在Mac系統(tǒng)上完成的梯捕,畢竟 大豬 工作多年之后終于買得起一個Mac了。
- 創(chuàng)建
React-Native
項(xiàng)目
react-native init dounineApp
- 安裝
code-push-cli
npm install -g code-push-cli
- 注冊
code-push
帳號
code-push register
Please login to Mobile Center in the browser window we've just opened.
Enter your token from the browser:
#會彈出一個瀏覽器,讓你注冊,可以使用github帳號對其進(jìn)行授權(quán),授權(quán)成功會給一串Token,點(diǎn)擊復(fù)制窝撵,在控制進(jìn)行粘貼回車(或者使用code-push login命令)傀顾。
Enter your token from the browser: b0c9ba1f91dd232xxxxxxxxxxxxxxxxx
#成功提示如下方
Successfully logged-in. Your session file was written to /Users/huanghuanlai/.code-push.config. You can run the code-push logout command at any time to delete this file and terminate your session.
- 在
code-push
添加一個ios的app
code-push app add dounineApp-ios ios react-native
#成功提示如下方
Successfully added the "dounineApp-ios" app, along with the following default deployments:
┌────────────┬──────────────────────────────────────────────────────────────────┐
│ Name │ Deployment Key │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Production │ yMAPMAjXpfXoTfxCd0Su9c4-U4lU6dec4087-57cf-4c9d-b0dc-ad38ce431e1d │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Staging │ IjC3_iRGEZE8-9ikmBZ4ITJTz9wn6dec4087-57cf-4c9d-b0dc-ad38ce431e1d │
└────────────┴──────────────────────────────────────────────────────────────────┘
- 繼續(xù)在
code-push
添加一個android的app
code-push app add dounineApp-android android react-native
#成功提示如下方
Successfully added the "dounineApp-android" app, along with the following default deployments:
┌────────────┬──────────────────────────────────────────────────────────────────┐
│ Name │ Deployment Key │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Production │ PZVCGLlVW-0FtdoCF-3ZDWLcX58L6dec4087-57cf-4c9d-b0dc-ad38ce431e1d │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Staging │ T0NshYi9X8nRkIe_cIRZGbAut90a6dec4087-57cf-4c9d-b0dc-ad38ce431e1d │
└────────────┴──────────────────────────────────────────────────────────────────┘
- 在項(xiàng)目根目錄添加
react-native-code-push
npm install react-native-code-push --save
#或者
yarn add react-native-code-push
- link react-native-code-push
react-native link
Scanning folders for symlinks in /Users/huanghuanlai/dounine/oschina/dounineApp/node_modules (8ms)
? What is your CodePush deployment key for Android (hit <ENTER> to ignore) T0NshYi9X8nRkIe_cIRZGbAut90a6dec4087-57cf-4c9d-b0dc-ad38ce431e1d
#將剛才添加的Android App的Deployment Key復(fù)制粘貼到這里,復(fù)制名為Staging測試Deployment Key。
rnpm-install info Linking react-native-code-push android dependency
rnpm-install info Android module react-native-code-push has been successfully linked
rnpm-install info Linking react-native-code-push ios dependency
rnpm-install WARN ERRGROUP Group 'Frameworks' does not exist in your Xcode project. We have created it automatically for you.
rnpm-install info iOS module react-native-code-push has been successfully linked
Running ios postlink script
? What is your CodePush deployment key for iOS (hit <ENTER> to ignore) IjC3_iRGEZE8-9ikmBZ4ITJTz9wn6dec4087-57cf-4c9d-b0dc-ad38ce431e1d
#繼續(xù)復(fù)制Ios的Deployment Key
Running android postlink script
- 在
react-native
的App.js
文件添加自動更新代碼
import codePush from "react-native-code-push";
const codePushOptions = { checkFrequency: codePush.CheckFrequency.MANUAL };
export default class App extends Component<{}> {
componentDidMount(){
codePush.sync({
updateDialog: true,
installMode: codePush.InstallMode.IMMEDIATE,
mandatoryInstallMode:codePush.InstallMode.IMMEDIATE,
//deploymentKey為剛才生成的,打包哪個平臺的App就使用哪個Key,這里用IOS的打包測試
deploymentKey: 'IjC3_iRGEZE8-9ikmBZ4ITJTz9wn6dec4087-57cf-4c9d-b0dc-ad38ce431e1d',
});
}
...
- 運(yùn)行項(xiàng)目在ios模擬器上
react-native run-ios
如圖下所顯
1:開啟debug調(diào)試
2:CodePush
已經(jīng)成功運(yùn)行
目前App已經(jīng)是最新版本
- 發(fā)布一個ios新版本
code-push release-react dounineApp-ios ios
發(fā)布成功如圖下
Detecting ios app version:
Using the target binary version value "1.0" from "ios/dounineApp/Info.plist".
Running "react-native bundle" command:
node node_modules/react-native/local-cli/cli.js bundle --assets-dest /var/folders/m_/xcdff0xd62j4l2xbn_nfz00w0000gn/T/CodePush --bundle-output /var/folders/m_/xcdff0xd62j4l2xbn_nfz00w0000gn/T/CodePush/main.jsbundle --dev false --entry-file index.js --platform ios
Scanning folders for symlinks in /Users/huanghuanlai/dounine/oschina/dounineApp/node_modules (10ms)
Scanning folders for symlinks in /Users/huanghuanlai/dounine/oschina/dounineApp/node_modules (10ms)
Loading dependency graph, done.
bundle: start
bundle: finish
bundle: Writing bundle output to: /var/folders/m_/xcdff0xd62j4l2xbn_nfz00w0000gn/T/CodePush/main.jsbundle
bundle: Done writing bundle output
Releasing update contents to CodePush:
Upload progress:[==================================================] 100% 0.0s
Successfully released an update containing the "/var/folders/m_/xcdff0xd62j4l2xbn_nfz00w0000gn/T/CodePush" directory to the "Staging" deployment of the "dounineApp-ios" app.
- 重新Load刷新應(yīng)用
- 安卓發(fā)布
與上面9~11步驟是一樣的碌奉,命令改成Android對應(yīng)的,以下命令結(jié)果簡化
1.修改App.js的deploymentKey為安卓的
deploymentKey:'T0NshYi9X8nRkIe_cIRZGbAut90a6dec4087-57cf-4c9d-b0dc-ad38ce431e1d'
2.運(yùn)行
react-native run-android
3.發(fā)布
code-push release-react dounineApp-android android
4.刷新應(yīng)用,如下圖
福利 項(xiàng)目源碼