RN热更新MAC端实现

2019-01-08  本文已影响0人  三三哥

前提是npm,react-native等环境已经安装好!!!

1.创建React-Native项目

react-native init lshApp(自己取,不要这个一样,因为已经被用过了)

2. 安装 code-push-cli

npm install -g code-push-cli

3.注册code-push 账号

code-push register 

自动弹出一个浏览器,可以使用github 账号进行注册,成功之后拿到token

Enter your token from the browser: *********

4. 把自己的app添加到code-push

code-push app add  lshApp-ios ios react-native(友情提示不要开代理,公司网线就可以)

Successfully added the "lshApp-ios" app, along with the following default deployments:

┌────────────┬──────────────────────────────────────────────────────────────────┐

│ Name      │ Deployment Key                                                  │

├────────────┼──────────────────────────────────────────────────────────────────┤

│ Production │ UfIyRgZDBdUcV0sJD1gHC5e__HBe33654068-8807-4f4a-b8d9-e37a81d3017f │

├────────────┼──────────────────────────────────────────────────────────────────┤

│ Staging    │ BQlZi_rKmiY2kYVdqy_jdWA8vHAR33654068-8807-4f4a-b8d9-e37a81d3017f │

└────────────┴──────────────────────────────────────────────────────────────────┘

code-push app add  lshApp-android android react-native (安卓)

➜  RN_Update code-push app add lshApp-android android react-native

Successfully added the "lshApp-android" app, along with the following default deployments:

┌────────────┬──────────────────────────────────────────────────────────────────┐

│ Name      │ Deployment Key                                                  │

├────────────┼──────────────────────────────────────────────────────────────────┤

│ Production │ ej3yTpfDUeUeOYpYi4OKukk7s0kx33654068-8807-4f4a-b8d9-e37a81d3017f │

├────────────┼──────────────────────────────────────────────────────────────────┤

│ Staging    │ ebNHjs6SfRd4pUttaMPvoK7OQqjr33654068-8807-4f4a-b8d9-e37a81d3017f │

5.在项目根目录添加 react-native-code-push

npm install react-native-code-push --save

#或者

yarn add react-native-code-push

6. react-native link

添加安卓的staging key ebNHjs6SfRd4pUttaMPvoK7OQqjr33654068-8807-4f4a-b8d9-e37a81d3017f

依次添加iOS staging key BQlZi_rKmiY2kYVdqy_jdWA8vHAR33654068-8807-4f4a-b8d9-e37a81d3017f 

7.在 react-native App.js 添加如下代码

export default class App extends Component<Props> {

  componentDidMount(){

    codePush.sync({

      updateDialog: true,

      installMode: codePush.InstallMode.IMMEDIATE,

      mandatoryInstallMode:codePush.InstallMode.IMMEDIATE,

      //deploymentKey为刚才生成的,打包哪个平台的App就使用哪个Key,这里用IOS的打包测试

      deploymentKey: 'BQlZi_rKmiY2kYVdqy_jdWA8vHAR33654068-8807-4f4a-b8d9-e37a81d3017f',

      });

  }

8. 运行模拟器 react-native run-ios (不成功,关掉本地RN自动打开的终端,删除模拟器上的项目,关掉模拟器,多试几次,开代理)

9.发布一个新版本

  code-push release-react lshApp-ios iOS

10. 重新刷新应用

11.安卓发布修改下 deploymentKey: 'BQlZi_rKmiY2kYVdqy_jdWA8vHAR33654068-8807-4f4a-b8d9-e37a81d3017f',

然后按照8-11再来一遍

参考此文章,https://blog.csdn.net/dounine/article/details/78529106 并且填一些自己遇到的坑!

上一篇下一篇

猜你喜欢

热点阅读