這幾天經歷了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));
下載問題解決可能你還會發現你無法直接打開本地下載的apk進行安裝(華為手機親測會有的問題)
你需要在platforms\android\app\src\main中的AndroidManifest.xml中添加
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
以上就是博主碰見的幾個問題,希望可以幫助大家
12.23補充 如果上面所有條件都滿足但是仍然不行,可能是sdk太高的問題,可以適當降低sdk