上文已经成功把RN植入到项目中了,下面就是再把热更新功能加上去。
1、codePush把依赖库下载到项目中
cd ReactComponent文件所在路径
npm install --save react-native-code-push
下载完毕后会在mode_modules文件里会发现多了一个文件
![](https://img.haomeiwen.com/i1307506/9369cd375c396bdb.png)
2、建立关联
podfile文件添加一项:
pod 'CodePush', :path => './ReactComponent/node_modules/react-native-code-push/CodePush.podspec'
终端敲命令
cd 项目路径
pod install
添加.a文件
![](https://img.haomeiwen.com/i1307506/52509ceac281e800.png)
3、更新策略:(在你所需要更新的界面的js中添加)
import codePush from 'react-native-code-push';
一个简单暴力的:
constructor(props){
super(props);
codePush.sync({
updateDialog:{
appendReleaseDescription:true,
descriptionPrefix:'\n\n更新内容:\n',
title:'更新',
mandatoryUpdateMessage:'',
mandatoryContinueButtonLabel:'更新',
},
mandatoryInstallMode:codePush.installMode,
deploymentKey:'_TmRhR6-KeBNY9f98bf-4ac2-be16-8f489592e1ef'
});
}
4、codepush新建项目并且上传boundle文件
(4.1)注册app(终端书命令)
code-push app add rntestApp ios react-native
把返回的信息(key)记录下来(一个开发测试的,一个正式的)
(4.2)打包上传包
1.
cd ReactComponent文件所在路径
2.
code-push release-react rntestApp ios --t 1.0.0 --dev false --d Staging --des "第一版" --m false
(4.3)设置测试的key
![](https://img.haomeiwen.com/i1307506/0c08c1f98582cd02.png)
注意点:
使用
code-push release-react rntestApp ios
打包更新是时
终端会提示失败信息(目前我只能有这种方法解决,如果有更好方法的同学欢迎提出)
Unable to find either of the following plist files in order to infer your
app's binary version: "ios/rntestApp/Info.plist", "ios/Info.plist". If your
plist has a different name, or is located in a different directory, consider
using either the "--plistFile" or "--plistFilePrefix" parameters to help
inform the CLI how to find it.
原因是找不到info.plist文件,如果是标准的React Native项目是不会有这种提示的,因为标准的React Native的项目结构中ios的项目启动的文件是在react native文件的内部的,而现在的项目结构是ios项目文件包含了react native的资源文件,总体来讲就是一个包含和被包含的关系的区别。既然缺少plist文件我们可以在对应路径上添加一个info.plist文件,其主要访问的是app的版本号和开发的key(之前有过添加截图):具体做法是copy一份info.plist文件把它放在
ReactComponent/ios的路径下
然后再次执行
code-push release-react rntestApp ios
命令即可
第二种方式(如上面的方式出错)
通过上传包来进行更新
--离线包--
$ cd ./工程目录
$ mkdir bundles
$ react-native bundle --platform 平台 --entry-file 启动文件 --bundle-output 打包js输出文件 --assets-dest 资源输出目录 --dev 是否调试。
eg.
$react-native bundle --platform ios --entry-file index.js --bundle-output ./ios/bundles/main.jsbundle --dev false
--发布更新--
$ code-push release <应用名称> <Bundles所在目录> <对应的应用版本> --deploymentName: 更新环境 --description: 更新描述 --mandatory: 是否强制更新
code-push release HotUpdateDemo-ios ./ios/bundles/main.jsbundle 1.0.0 --deploymentName Production --description "我是新包,很新的那种" --mandatory true
网友评论