How open link in safari mobile app from webview

纵然是瞬间 提交于 2021-01-21 04:24:05

问题


There are many topics of this here, but they all need native code interaction to work.

In my case, it is necessary to be able to do it directly from the url, without any interaction with my mobile app.

I tried:

<a href="safari://google.com" target="_blank">Open Google in Safari</a>

and

<a href="webkit://google.com" target="_blank">Open</a>

and based in this post.

<script>
    $(document).on('click', 'a[target="_blank"]', function (ev) {
      var url;

      ev.preventDefault();
      url = $(this).attr('href');
      window.open(url, '_system');
    });
  </script>

but nothing works.

Anyone have any idea how to fix this?


回答1:


There is a trick. We know iOS Safari have these available URL Schemes:

(HTTP) — http://websiteurl (HTTPS) — https://websiteurl x-web-search:// (FTP) — ftp://locationtofileonftpserver

If you use Click here or window.open("http://somewebsite"). It always use current browser to open url.

x-web-search://?[keyword] - It will switch into Safari app but search for the keyword

Luckily we still have ftp:// left. It will switch to Safari app. But first you need to setup a public folder in your hosting & create a bridge html file to redirect user back to http:

ftp://{youripaddress}/bridge.html

window.open("https://yoururl", "_self");

Now you can open your website in the default Safari app from any browsers.

The original answer is here: JS - Mobile - Open Safari from any browser




回答2:


There isn't a URL Scheme for Safari on iOS.

See Apple's Documentation: https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/Introduction/Introduction.html

Have a search around and you will see similar answers: What is Mobile Safari's custom URL Scheme?




回答3:


If this is running in safari it should comply with safari async call restrictions regarding popups as explained here.

You should fix your code so that the window open will be outside the function, Something like that:

    <script>
    var windowReference = window.open();

    $(document).on('click', 'a[target="_blank"]', function (ev) {
      var url;

      ev.preventDefault();
      url = $(this).attr('href');
      windowReference.location = url;
    });
  </script>


来源:https://stackoverflow.com/questions/45378919/how-open-link-in-safari-mobile-app-from-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!