33

iOS URL Schemes allow web sites to launch apps like so:

  • twitter://timeline launches Twitter
  • googlechrome://google.com launches Chrome
  • fb://root launches Facebook
  • ______________ launches Safari? (not http://, since Safari won't launch from UIWebView)

What custom url scheme triggers Safari to launch (even from within another app's UIWebView)?

To clarify, I'm not looking for [[UIApplication sharedApplication] openURL: request.URL];

Instead I'm looking for how a website can allow a user to launch Mobile Safari from within the UIWebView of another app (Google Chrome, Twitter, etc.).

Example HTML links that pop open other apps:

<a href="twitter://timeline">Open Twitter</a>
<a href="googlechrome://google.com">Open site in Chrome</a>
<a href="fb://root">Open Facebook</a>

I'm looking for something similar to these non-working examples:

<a href="safari://google.com">Open Safari [Doesn't work]</a>
<a href="webkit://google.com">Open Webkit [Doesn't work]</a>

Here's a jsFiddle of the same: http://jsfiddle.net/gXLjF/9/embedded/result/

Try opening this URL in iOS Google Chrome and opening Safari with the links.

Ryan
  • 14,682
  • 32
  • 106
  • 179
  • 3
    The only way to launch Safari is to use `UIApplication openURL:` and pass an http or https URL. – rmaddy Mar 25 '14 at 17:39
  • 1
    That's using a `UIWebView` within the app. That is the opposite of launching mobile Safari. – rmaddy Mar 25 '14 at 17:41
  • All http URLs on iOS open in Mobile Safari by default when you use `openURL` – Adam Jenkins Mar 25 '14 at 17:42
  • @Adam, except when launched from another app inside a `UIWebView`. – Ryan Mar 25 '14 at 17:59
  • @rmaddy, yes, I understand. I'm looking for how to break out of a UIWebView within another app that I have no control of (let's say Google Chrome) and launch the url in Mobile Safari instead. – Ryan Mar 25 '14 at 18:00
  • That can't be done and that's a good thing. Let the user choose the browser. Your website shouldn't force the browser being used. Can you explain why you want to force a user to Safari from whatever app they are using? – rmaddy Mar 25 '14 at 18:05
  • @rmaddy, yes, the UIWebView is limited and disables iOS features like "Add to Passbook". In principle I agree with you. But I'm trying to provide the user with a choice — continue to operate within the UIWebView with a limited experience, or allow them to break out of the "frame" and take advantage of the full experience. Ex: http://jsfiddle.net/gXLjF/embedded/result/ – Ryan Mar 25 '14 at 18:16
  • 1
    Did you find any solution to this? I'm having a similar issue with the Facebook inApp Browser which is breaking my PayPal checkout.... grrrrrrr – Emil Borconi Jun 30 '16 at 13:44
  • 1
    atm the best solution is to use `x-web-search://?` or `x-web-search://?your-keyword` – gondo Jun 11 '20 at 07:28

1 Answers1

3

There is no Safari URL scheme. If you make one up and use it in your html you can check for it though.

Implement the UIWebViewDelegate method webView:shouldStartLoadWithRequest:navigationType:. Return 'NO' for the requests that you want to shunt to mobile safari. Call UIApplication openURL with the request's URL.

Something like this:

- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    // all clicked links!
    if ( navigationType == UIWebViewNavigationTypeLinkClicked )
    {
        [[UIApplication sharedApplication] openURL: request.URL];
        return NO;
    }

    // or, custom URL scheme!
    if ( [request.URL.scheme isEqualToString: @"my-open-in-safari"] )
    {
        // remap back to http.  untested!
        NSURL* url = [NSURL URLWithString: [request.URL.absoluteString stringByReplacingOccurrencesOfString: @"my-open-in-safari" withString: @"http" ]];

        [[UIApplication sharedApplication] openURL: url];
        return NO;
    }

    return YES;
}
TomSwift
  • 39,369
  • 12
  • 121
  • 149
  • So there's no way you can use this from the web then. For example, if I wanted to allow users to break out of an in-app `UIWebView` into Mobile Safari. Are you aware if `UIWebView`s support `x-callback-url`? I've been reading about hacks that use this toggle between different apps. – Ryan Mar 25 '14 at 20:09
  • 1
    I'm not following your question. This IS for breaking out of a UIWebView into Mobile Safari. No; I'm not aware of x-callback-url. – TomSwift Mar 25 '14 at 20:11
  • 2
    Yes, the above *is* for breaking out of a UIWebView within an app that you control (i.e. you are the developer). But this does not work for websites to allow their users to break out of a framed experience (let's say in Twitter's app — an app they do not control the source code of). – Ryan Mar 25 '14 at 20:22
  • @Ryan Did you ever find a workaround for this ? – Mandeep Singh Jul 11 '17 at 06:23