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熱部署的一次實踐