26

Is there a URI scheme to open Mobile Safari to a specific URL?

e.g.,

<a href="safari://open_url/?url=google.com">Open Google in Safari</a>
Kirk Ouimet
  • 27,280
  • 43
  • 127
  • 177

5 Answers5

5

Update: So, as of iOS 6.0.2, there is still isn't a URL scheme specific to MobileSafari (see below). However, Federico Viticci has posted an interesting hack that will allow you to call Safari from Chrome for iOS. It's not as functional as a Chrome-to-Safari bookmarklet, but it does show it's possible to launch MobileSafari from Chrome for iOS.

Original Answer: It turns out you can't open a link in Safari using just a URI scheme. Hyperlinks in other apps can be opened in safari using openURL (see other answers), but there is no scheme for MobileSafari itself (which you would need if you were to open a link in Safari using a hyperlink in Chrome or Opera for iOS).

Google Chrome has the following two URI schemes: googlechrome:// and googlechromes:// (for HTTPS) that work just as any other app-specific scheme (such as dayone://, things://, or sms://).

Dave
  • 2,396
  • 2
  • 22
  • 25
3

OK, I think I understand what you are asking for.

Have a class implement the UIWebViewDelegate protocol. Implement the method

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

This method gets called whenever the user clicks a link in your webview. You can get the URL the user clicked on be inspecting the request parameter. You can then intercept any links you want to open in mobile Safari rather then inside your webView and then call -openURL: on the shared application as in my other answer.

[[UIApplication sharedApplication] openURL:URLFromRequest];
Simon Goldeen
  • 9,080
  • 3
  • 36
  • 45
2

Try this:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com/"]];
Simon Goldeen
  • 9,080
  • 3
  • 36
  • 45
  • 1
    I need the link to be in HTML, not client app code. Updating the question to be more clear - sorry Simon! – Kirk Ouimet Apr 30 '12 at 21:01
  • 1
    I think Kirk is looking for a specific URL scheme (akin to the URL scheme used to open preferences before 5.1: `[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];`). – Reed Olsen Apr 30 '12 at 21:02
  • @Kirk I just edited my answer, if you open a URL with the HTTP scheme, it will use mobile safari. – Simon Goldeen Apr 30 '12 at 21:02
1
window.location.replace("x-web-search://?your search here");

URI Scheme for safari is- x-web-search://

This will open your search in safari browser.

  • 2
    do you know if this works on current versions of iOS (e.g., 12.4 ?) I see `x-web-search` suggested across the web but I suspect this scheme might be deprecated. – felguerez Jul 23 '19 at 14:14
0

You can also use the webkit javascript bridge https://github.com/marcuswestin/WebViewJavascriptBridge

This allows you to send messages from javascript to xcode. I use this to close the webview from the html page:

self.bridge = [WebViewJavascriptBridge bridgeForWebView:self.webView handler:^(id data, WVJBResponseCallback responseCallback) {
            NSLog(@"Received message from javascript: %@", data);
            if ([data isEqual:@"closewin!"])
                [self dismissModal];
        }];

but you could also use this to jump to safari or anything you want

woens
  • 1,058
  • 9
  • 20