react-native更新App(安卓+ios)
1.Android直接和服务器上版本号作对比下载APK进行安装
2.iOS由于需要到AppStore进行下载应用,所以应用需要和AppStore上应用做版本对比后跳转到AppStore进行App更新
安卓:
1、使用插件 rn-fetch-blob //或者 react-native-fetch-blob
npm install --save rn-fetch-blob //安装插件
//或
yarn add rn-fetch-blob
2、安卓配置
在android/app/src/main/AndroidManifest.xml添加权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
3、ios配置
cd ios
pod install //ios使用pod安装
使用
1、安卓使用
/***
Android6.0+获取文件权限
*/
crequestMultiplePermission = async () => {
try {
const permissions = [
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
];
const granteds = await PermissionsAndroid.requestMultiple(permissions);
if (granteds['android.permission.WRITE_EXTERNAL_STORAGE'] === 'granted') {
this.storage = true;
} else {
this.storage = false;
}
if (this.storage) return;
} catch (err) {
console.log(err);
}
};
import RNFetchBlob from 'rn-fetch-blob';
Update = () => {
const android = RNFetchBlob.android;
//配置手机系统通知栏下载文件通知,下载成功后点击通知可运行apk文件
RNFetchBlob.config({
addAndroidDownloads: {
useDownloadManager: true,
title: 'update 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',
'https://raw.githubusercontent.com/feicien/android-auto-update/develop/extras/android-auto-update-v1.2.apk' //apk下载地址
).then(res => {
//下载成功后自动打开安装已下载apk文件
android.actionViewIntent(
res.path(),
'application/vnd.android.package-archive'
);
});
}
2、IOS使用
将ios目录下ios_upgrade文件夹拷贝到项目目录下
import { NativeModules } from "react-native";
NativeModules.upgrade.upgrade("123456", msg => {
if (msg === "YES") {
// 跳转到APP Stroe
NativeModules.upgrade.openAPPStore("123456") //数字为对应的AppStore引用的ID
} else {
// TODO: 无更新
callback && callback();
}
}
ios不使用插件更新使用如下:
Linking.openURL("APPstore里面appId").catch(err => console.error('An error occurred', err))