需求:安卓和IOS開發的混合app,前端使用vue,vant2,安卓使用java,ios使用的object-c,實作效果:點擊按鈕,下載PDF附件,app跳轉到手機外部瀏覽器,下載附件......
1,安卓端代碼:
public static void openPDFInBrowser(Context context, String url) { Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, uri); intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); try { context.startActivity(intent); } catch (ActivityNotFoundException e) { Log.w("error", "Activity was not found for intent, " + intent.toString()); } }
2,IOS端代碼:
[[UIApplication sharedApplication]openURL:URL options:@{} completionHandler:^(BOOL success) {
}];
函式異步執行,在主佇列中呼叫 completionHandler 中的回呼,
引數:
openURL:打開的網址
options:用來校驗url和applicationConfigure是否配置正確,是否可用,
如果校驗為不可用,completionHandler的回呼success為NO,
唯一可用@{UIApplicationOpenURLOptionUniversalLinksOnly:@YES},
不需要就用@{}為置空,不能直接置nil,
置空將不會校驗,completionHandler的回呼success恒為YES,
ompletionHandler:如不需要可置nil
3,前端代碼使用橋接:
_download(){ const that = this; const {voucherNo,unionid,custId} = this; const baseUrl = config.BASE_API; const url = `/api/wx/down/voucherDetailByB001?voucherId=${voucherNo}&unionId=${unionid}`; const downUrl = baseUrl + url; this.isLoading = true; this.loadingText = "下載中"; this.$JQAPI('saveFile', { param: {url: downUrl, suffix: 'pdf' }, successCallBack: function (res) { that.isLoading = false; const result = JSON.parse(res); console.log(result); if (result && result.code == 200) { const data = result.data; console.log(data); Toast({message:`檔案已保存至${data}檔案夾`,position: 'bottom',duration: 5000}); } else { that.$toast(result.msg); } }, failedCallBack: function (err) { console.log(err) that.$toast(err); that.isLoading = false; } });
注意:后端代碼回傳流,不能設定content-type:application/x-msdownload,否則IOS下載的附件會帶上.exe
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/544242.html
標籤:Android
下一篇:app實作外部瀏覽器打開鏈接
