8

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?

jose920405
  • 7,982
  • 6
  • 45
  • 71

3 Answers3

7

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!

Goon Nguyen
  • 1,462
  • 11
  • 26
  • Thanks for this. using ftp:// I was able to start Safari from facebook in app browser on IOS, however my safari is not able to read the bridge from the FTP. What is the correct configuration for the open FTP server? I tried with anonymous access to it but it it does not work. I tested the FTP with FileZlla and I can open the FTP as anonymous. – AleCat83 Sep 24 '19 at 16:21
  • Update (2021-01): Apple seems to fix this on iOS, this is no longer work! – Goon Nguyen Feb 01 '21 at 05:51
  • Was this fixed in a specific version of Safari IOS? currently working in my setup but my Safari is not updated. Is there some official documentation about it? – AleCat83 Feb 04 '21 at 18:20
  • 1
    tested this implementation today on iOS 14.4 and it is still working for me. – AleCat83 Feb 09 '21 at 16:56
  • 1
    iOS 15.1 broken – Stefan Sprenger Nov 11 '21 at 09:39
0

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>
ApriOri
  • 2,618
  • 29
  • 48
  • This is actually a step in the wrong direction. By placing the window.open outside the click event, you are violating the rule of by opening in a thread other than the one initiated by user interaction. FWIW it doesn't matter. As of at least the time of this writing with the latest iOS 13.1, OP's question still is not possible. – Ezekiel Victor Oct 07 '19 at 22:19
0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267