Android
配置
安装: yarn add rn-fetch-blob@0.10.15 --save
import RNFetchBlob from 'rn-fetch-blob';
/*低版本解决方案添加下面权限
import {PermissionsAndroid} from 'react-native';
_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);
}
};
*/
//更新Android.APK或IOS.IPA
_updateApk = (downloadUrl) => {
if (Platform.OS === 'android') {
this._toggleModal();
let android = RNFetchBlob.android;
RNFetchBlob.config({
addAndroidDownloads: {
useDownloadManager: true,
title: 'wpt.apk',
description: '安装wpt.apk',
mime: 'application/vnd.android.package-archive',
mediaScannable: true,
notification: true,
path: `${RNFetchBlob.fs.dirs.DownloadDir}/wpt.apk`
}
})
.fetch('GET', downloadUrl)
.then((res) => {
android.actionViewIntent(res.path(), 'application/vnd.android.package-archive');
});
} else {
this._toggleModal();
this.setState({ isActivity: true });
//AppId为上架后的应用的AppId可以到开发者中心中查看
NativeModules.upgrade.upgrade('AppId', (msg) => {
if (msg === 'YES') {
this.setState({ isActivity: false });
NativeModules.upgrade.openAPPStore('AppId');
} else {
this.setState({ isActivity: false });
ToastUtil.toastLong(I18n.t('LatestVersion'));
}
});
}
};
遇到的问题以及解决办法
Android 9 上无法使用android.actionViewIntent
自动安装应用
解决办法:
1、 修改 node_modules/rn-fetch-blob/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java
将 intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
替换为intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);
2、然后修改自己项目的AndroidManifest.xml
增加
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
IOS
配置
yarn add rn-app-upgrade --save
// 低于0.6+版本
react-native link rn-app-upgrade
将node_modules/ios_upgrade
里面的文件拷贝到项目的IOS目录下
打开Xcode将这两个文件导入项目工程----->点击右键 Add filfe…
即可跳转AppStore进行更新(
代码在安卓中已做兼容处理
)