jose920405
jose920405

Reputation: 8057

How open link in safari mobile app from webview

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?

Upvotes: 10

Views: 19784

Answers (3)

Goon Nguyen
Goon Nguyen

Reputation: 1520

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


Update (2021-01): Apple seems to fix this on iOS, this is no longer work!

Upvotes: 7

Vincent Ritter
Vincent Ritter

Reputation: 21

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?

Upvotes: 0

ApriOri
ApriOri

Reputation: 2688

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>

Upvotes: 0

Related Questions