2

I am trying to open a link forcefully into safari in iPhone, but I am not able to do so.

So my requirement is I am using WebRTC's getUserMedia to access the video from iPhone camera and with Safari it is working properly, but when I try to run this on Chrome getUserMedia isn't working. I have tried this example of WebRTC also in chrome, but it's not working.

So now, I want to open the link which is shared through email always with Safari.

I have tried this link, but they are not working -

Force link to open in mobile safari from a web app with javascript

Also, in case of Safari URL Schemes didn't work so not able to use that as well.

Any suggestions or help is highly appreciated.

Rajat
  • 10,977
  • 3
  • 38
  • 55

1 Answers1

1

I found the answer! From here https://gist.github.com/kylebarrow/1042026

This script in the head

<script>
// Mobile Safari in standalone mode
if(("standalone" in window.navigator) && window.navigator.standalone){

    // If you want to prevent remote links in standalone web apps opening Mobile Safari, change 'remotes' to true
    var noddy, remotes = false;

    document.addEventListener('click', function(event) {

        noddy = event.target;

        // Bubble up until we hit link or top HTML element. Warning: BODY element is not compulsory so better to stop on HTML
        while(noddy.nodeName !== "A" && noddy.nodeName !== "HTML") {
            noddy = noddy.parentNode;
        }

        if('href' in noddy && noddy.href.indexOf('http') !== -1 && (noddy.href.indexOf(document.location.host) !== -1 || remotes))
        {
            event.preventDefault();
            document.location.href = noddy.href;
        }

    },false);
}
</script>

and standard HTML for the links

<a href="http://extneral" target="_blank">your link</a>

This way local links under the web apps still open in the same window, but the external http:// links will open in Safari

karthik
  • 1,100
  • 9
  • 21
  • Hi @karthik, thanks for your suggestion. I have tried it but it's not working, it is opening the link in same browser. – Rajat Jul 04 '18 at 09:51
  • i'm worried there is no workaround if you are in one browser and want to move to some other browser in javaScript. As every browser works in a sandbox environment its even a security violation @rajat – karthik Jul 04 '18 at 10:02
  • 1
    @karthik not exactly - you can open Chrome from Safari using Universal Links technique: `location.href="googlechrome" + location.href.substring(4)` – hamczu Sep 25 '18 at 09:27
  • 11
    @hamczu do you know what is Safari's URL scheme on iOS? – Goon Nguyen Oct 26 '18 at 11:41
  • There is no Safari Url scheme. – Stefan Sprenger Feb 28 '22 at 17:51