9

I am having some issues with my webapp where if the user clicks the link from Facebook app it will use it's own browser/webview, but it is required to use Safari or Chrome in order to run the page.

While researching (this issue), I found that you can use googlechrome://navigate?url=example.com from a Android device to open chrome, and googlechrome://myurl.com from a iOS to open Chrome aswell, but I need a way to open Safari if the user's device is iOS.

Is there a way to do so?

Ps.: The link the user clicks in Facebook goes to a landing page, and in this page there is a button to the webapp. When the user clicks this button I will open Chrome/Safari.

Thanks in advance!

1 Answers1

1

There are a few answers across SO with a similar topic. Something along the lines of

NSString *yourUrl = @"http://yourURL";
SFSafariViewController *svc= [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString: yourUrl]];
[self presentViewController:svc animated:YES completion:nil];

In Swift, that's

let yourUrl = "http://www.yourURL.com";
let svc = SFSafariViewController.init(url: URL);
svc.delegate = self
present(svc, animated: true);

should work.

If you're using Javascript, take a look at Force link to open in mobile safari from a web app with javascript

It seems you need to do something like

HTML:

<div id="foz" data-href="http://www.google.fi">Google</div>

JS:

document.getElementById("foz").addEventListener("click", function(evt) {
    var a = document.createElement('a');
    a.setAttribute("href", this.getAttribute("data-href"));
    a.setAttribute("target", "_blank");

    var dispatch = document.createEvent("HTMLEvents");
    dispatch.initEvent("click", true, true);
    a.dispatchEvent(dispatch);
}, false);

Test it at (http://www.hakoniemi.net/labs/linkkitesti.html)

Sources:

Open links in Safari instead of UIWebVIew?

How to force links to open in iOS Safari?

https://www.hackingwithswift.com/example-code/uikit/how-to-use-sfsafariviewcontroller-to-show-web-pages-in-your-app

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

rma
  • 1,853
  • 1
  • 22
  • 42