The app implements external browsers to open links

Requirements: A hybrid app developed for Android and IOS. The front end uses vue, vant2, Android uses java, and ios uses object-c. Achievement effect: click the button, download the PDF attachment, the app jumps to the mobile phone's external browser, download the attachment...

1. Android code:

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 terminal code:

\[\[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. The front-end code uses bridging:

 \_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;
        }
      });

Note: The back-end code returns the stream, and content-type: application/x-msdownload cannot be set, otherwise the attachment downloaded by IOS will bring .exe

Finally, if you have any good learning methods or suggestions, please leave a positive message in the comments. I hope everyone can learn, work together, and make progress together.

The editor is here to wish you all the promotion and salary increase in the days to come, becoming the general manager, becoming the CEO, marrying Bai Fumei, and reaching the pinnacle of life! !

No matter what difficulties we encounter, it should not be a reason for us to give up!

Many people always encounter some problems when they first come into contact with this industry or when they encounter a bottleneck period. Follow my homepage for the sorted out learning materials or click to scan the QR code below to get it for free~

Here is about my own Android learning, interview documents, and video collection . Those who are interested can take a look~

If you read this and think the article is well written, give it a thumbs up? If you think there is something worth improving, please leave me a message, and I will definitely check it carefully and correct the shortcomings, thank you.

Guess you like

Origin blog.csdn.net/m0_56255097/article/details/130367463