admin管理员组文章数量:1578658
一开始博主项目中做的是跳转到浏览器下载 后面发现有的手机跳转后无法连接服务器 但是在浏览器访问连接又可以 还没发现什么原因
后面试了3种解决方案 (改为 app内下载、更新)如下:
ios由于系统原因 只能跳转到 app stor 这里不讨论
注意:任何一种方式 都需要先获取存储读写权限,否则无法下载
方法1 先下载安装包,打开文件进行安装
- 使用dio工具库的下载,也可以采用下载插件flutter_downloader 使用稍微复杂点,这个插件支持Android、IOS下载
我这里演示代码使用的是dio下载 ,使用 install_plugin 或者 open_file 安装apk ,
我现在项目中使用的是 Dio 和 ota_update ,个人觉得这两个方便点
/// 下载apk
Future<File> _downloadApk(String datasource) async {
/// 创建文件
Directory storageDir = await getExternalStorageDirectory();
String storagePath = storageDir.path;
File file = new File('$storagePath/${Config.APP_NAME}v${_version}.apk');
/// 判断文件是否存在,不存在则创建文件
if (!file.existsSync()) {
file.createSync();
}
try {
/// 下载请求
Response response = await Dio().get(datasource,
onReceiveProgress: showDownloadProgress,
options: Options(
responseType: ResponseType.bytes,
followRedirects: false,
));
file.writeAsBytesSync(response.data);
return file;
} catch (e) {
print(e);
}
}
/// 安装apk
Future<Null> _installApk(String url) async {
File _apkFile = await _downloadApk(url);
String _apkFilePath = _apkFile.path;
if (_apkFilePath.isEmpty) {
print('确保设置了apk文件?');
return;
}
InstallPlugin.installApk(_apkFilePath, Config.APP_ID)
.then((result) {
print('install apk $result');
}).catchError((error) {
print('install apk error: $error');
});
}
方法2 使用 flutter_xupdate
传送门
方法3 使用 ota_update 第三方依赖
注意:安卓6.0以下 会出现无法下载或apk无效的情况 需自行进行配置
传送门
版权声明:本文标题:flutterAPP下载或 APP更新的几种方法 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1725437970a1023222.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论