題記 —— 執劍天涯,從你的點滴積累開始,所及之處,必精益求精,便是折騰每一天。java
重要消息android
本文章將講述: 在 flutter 跨平臺開發中,使用插件 install_plugin_custom,實如今 Android 平臺調起自動安裝,在 ios 平臺跳轉 appstore中更新ios
flutter 跨平臺實際應用開發中,app的升級效果以下 ios 平臺:git
Android 平臺:github
在APP開發方案中,通常咱們會經過訪問咱們的服務平臺來獲取 APP 的版本信息,如版本號、版本名稱、是否強制更新等等網絡
在這裏描述的是 在 Android 平臺下載 apk 而後再調起應用程序的安裝,在 ios 平臺點擊更新跳轉 appstore 平臺。app
在這裏下載 apk 使用的是 Dio ,安裝 apk 使用的是 install_plugin_custom 插件,flutter 項目中的依賴async
# 權限申請
permission_handler: 4.0.0
# 網絡請求
dio: ^2.1.2
# APP升級安裝組件 Android中調用自動安裝 apk的程序 ios 調用打開APPStore
install_plugin_custom:
git:
url: https://github.com/zhaolongs/install_plugin_custom.git
ref: master
複製代碼
Future<bool> _checkPermission(BuildContext context) async {
if (Theme.of(context).platform == TargetPlatform.android) {
PermissionStatus permission = await PermissionHandler() .checkPermissionStatus(PermissionGroup.storage);
if (permission != PermissionStatus.granted) {
Map<PermissionGroup, PermissionStatus> permissions =
await PermissionHandler() .requestPermissions([PermissionGroup.storage]);
if (permissions[PermissionGroup.storage] == PermissionStatus.granted) {
return true;
}
} else {
return true;
}
} else {
return true;
}
return false;
}
複製代碼
Future<String> _findLocalPath(BuildContext context) async {
final directory = Theme.of(context).platform == TargetPlatform.android
? await getExternalStorageDirectory() : await getApplicationDocumentsDirectory();
return directory.path;
}
複製代碼
//apk 網絡存儲連接
String apkNetUrl ="";
//手機中sd卡上 apk 下載存儲路徑
String localPath ="";
Dio dio = Dio();
//設置鏈接超時時間
dio.options.connectTimeout = 1200000;
//設置數據接收超時時間
dio.options.receiveTimeout = 1200000;
try {
Response response = await dio
.download(apkNetUrl, localPath
onReceiveProgress: (int count, int total) {
// count 當前已下載文件大小
// total 須要下載文件的總大小
});
if (response.statusCode == 200) {
print('下載請求成功');
//"安裝";
} else {
//"下載失敗重試";
}
} catch (e) {
//"下載失敗重試";
if (mounted) {
setState(() {});
}
}
複製代碼
//apk 的包名
String apkPackageName ="";
// 安裝
InstallPluginCustom.installApk(
localPath,
apkPackageName)
.then((result) {
print('install apk $result');
}).catchError((error) {
// "重試";
installStatues = 2;
setState(() {});
});
複製代碼
若是 flutter 項目是運行在 ios 手機中,那麼有更新信息的時候,直接跳轉 appstore 中應用程序的頁面更新ui
if (Theme.of(context).platform == TargetPlatform.iOS) {
InstallPluginCustom.gotoAppStore(
"https://apps.apple.com/cn/app/id1472328992");
}
複製代碼
在使用的時候直接替換這裏的跳轉的連接就好。url