react-native-code-push熱更新

code-push賬號

  • 1.安裝Code-Push的命令行工具:
npm install -g code-push-cli
  • 2.註冊
code-push register
  • 3.若已經註冊過,則直接登錄
code-push login

運行之後,會在瀏覽器中打開一個返回token的頁面
複製token值到控制檯登錄

  • 4.創建應用
code-push app add <Your APP Name> <OS(ios/anroid/windows/macOS)> <platform(Objective-C / Swift/react-native/cordova/xamarin)>

如:

code-push app add MyApp ios react-native

也可以到code-push 後臺管理:https://appcenter.ms/apps/create進行創建,
如下圖:

code-push001.png

 

code-push002.png

 

  • 5.查看所有應用
code-push app ls //code-push app list

code-push003.png

  • 6.查看你的deployment key
code-push deployment ls Delivery  -k

如下圖:

 

code-push006.png

  • 7.在Xcode中填寫你的key

     

    code-push005.png

  • 8.添加測試
code-push deployment add Delivery Test

使用react-native-code-push

  • 1.安裝
npm i react-native-code-push --save
react-native link react-native-code-push

注意:需要根據當前項目的react-native版本,指定特定版本,不指定則默認安裝最新版,
具體可參考官方說明文檔react-native-code-push
運行react-native link react-native-code-push 正常情況會引入相關的庫,如下圖:

code-push004.png

 

若沒有配置成功,可手動導入庫

  • 2.引入
import codePush from "react-native-code-push";
  • 3.組件中使用
App = codePush(App); // Wrap Your Component
codePush.checkForUpdate('Your Deployment Key').then((update)=>{
  // codePush.checkForUpdate().then((update)=>{
    console.log('update is ',update);
    if(!update){
       Alert.alert("提示","已是最新版本--",[
         {text:"Ok", onPress:()=>{
             console.log("點了OK");
           }}
       ]);
    }
    else{
      codePush.sync({
       deploymentKey: 'Your Deployment Key',
        updateDialog: {
          optionalIgnoreButtonLabel: '稍後',
          optionalInstallButtonLabel: '後臺更新',
          optionalUpdateMessage: '有新版本了,是否更新?',
          title: '更新提示'
        },
        installMode: codePush.InstallMode.IMMEDIATE
      });
    }
  });

注意,若使用react-native-navigation組件需要在screen註冊時,
用codePush包裹你的screen,
不然就會出現很噁心、很噁心的問題——APP更新成功,重啓之後又

回滾了,,,

回滾了,,,

回滾了,,,

真是心中一萬隻草泥馬奔騰而過啊~~~

具體如下:

Navigation.registerComponent('AboutUS', () => codePush(AboutUS));
  • 4.打包release
react-native bundle --entry-file index.ios.js --platform ios --dev false --bundle-output ios/ios.jsbundle
  • 5.發佈到code-push Staging
code-push release-react MyAPP ios
  • 6.從Staging發佈到正式環境
code-push promote Delivery Staging Production

或者直接發佈到正式環境

code-push release-react MyAPP ios -d Production

注意IOS是根據info中的版本號來決定是否更新,版本獨立不影響的位數是三位
即iOS版本都是獨立不影響,version=X.Y.Z,當codepush發佈更新時,只會影響與其X.Y.Z三位數完全相同的版本,其他版本不會受影響。

  • 7.查看歷史發佈版本
code-push deployment history MyAPP Staging

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章