reactnative实现app应用内更新

reactnative实现app应用内更新

最近用reactnative写app,需要有app升级功能。查看资料,很多都是热更新,需要在CodePush等框架注册账号,然后传包到他们系统,下载次数有限制。并不符合要求。
于是使用rn-fetch-blob下载安装包在运行替换。

安装 rn-fetch-blob

npm install rn-fetch-blob

or

yarn add rn-fetch-blob

安装 react-native-device-info

安装react-native-device-info获取当前版本号

npm install react-native-device-info

or

yarn add react-native-device-info

使用

import RNFetchBlob from 'rn-fetch-blob';
import DeviceInfo from 'react-native-device-info';

const Update = () => {
if(DeviceInfo.getVersion !== newVersion){
  const android = RNFetchBlob.android;
  //配置手机系统通知栏下载文件通知,下载成功后点击通知可运行apk文件
  RNFetchBlob.config({
    addAndroidDownloads: {
      useDownloadManager: true,
      title: '更新app',
      description: 'An APK that will be installed',
      mime: 'application/vnd.android.package-archive',
      path: `${RNFetchBlob.fs.dirs.DownloadDir}/update.apk`,
      mediaScannable: true,
      notification: true
    }
  }).fetch(
    'GET',
     url, //apk下载地址
  ).progress({ interval: 250 }, (received, total) => {
    setpercent(received / total)// 下载进度
}).then(res => {
  setpercent(100)
    //下载成功后自动打开安装已下载apk文件
    console.log('要运行咯。。。。。');
    android.actionViewIntent(
      res.path(),
      'application/vnd.android.package-archive'
    );
  }).catch((err) => {
    console.log('err',err);
    
  })
}
}
  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值