这几天经历了ionic 4 更新apk的摧残,走了十万八千里的弯路,在这里分享一下
首先请大家先去 https://www.cnblogs.com/loaderman/p/11333193.html
如果还出现了问题再请观看本文
当你下载没有反应时,如果无法从服务器获取到请求,请先查看url是否正确
同时Android9.0以上的手机无法连接http,只能用https,请改用https的服务器,使用https下载时用域名进行下载
如果可以从服务器获取请求请更换下载插件为Downloader
1 ionic cordova plugin add integrator-cordova-plugin-downloader
2 npm install @ionic-native/downloader
在module.ts中
1 import { Downloader } from '@ionic-native/downloader/ngx';
2 providers: [
3 ...
4 Downloader,
5 ...]
在downloadApp()中代码为
1 var request: DownloadRequest = {
2 uri: 'https://url/test.apk',
3 title: 'Download',//下载任务名字
4 description: '',
5 mimeType: '',
6 visibleInDownloadsUi: true,
7 notificationVisibility: NotificationVisibility.VisibleNotifyCompleted,
8 destinationInExternalFilesDir: {
9 dirType: 'Download',//下载文件夹名字
10 subPath: 'test.apk'//下载的apk
11 }
12 };
13
14
15 let options: DocumentViewerOptions = {
16 title: 'MyApp'
17 }
18 this.downloader.download(request)
19 .then((location: string) =>
20 this.fileOpener.showOpenWithDialog(location, 'application/vnd.android.package-archive')//此处location为当前手机下载的地址
21 .then(() => alert('File is opened'))
22 .catch(e => console.log('Error opening file', e)))
23 .catch((error: any) => alert(error));
扫描二维码关注公众号,回复:
7754871 查看本文章
下载问题解决可能你还会发现你无法直接打开本地下载的apk进行安装(华为手机亲测会有的问题)
你需要在platforms\android\app\src\main中的AndroidManifest.xml中添加
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
以上就是博主碰见的几个问题,希望可以帮助大家