How can I launch Safari from an iPhone app?
Asked Answered
H

7

129

This might be a rather obvious question, but can you launch the Safari browser from an iPhone app?

Hitormiss answered 4/5, 2009 at 23:23 Comment(0)
A
200

should be the following :

NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];

if (![[UIApplication sharedApplication] openURL:url]) {
    NSLog(@"%@%@",@"Failed to open url:",[url description]);
}
Atrocious answered 27/8, 2009 at 19:40 Comment(9)
Will this count towards your app's memory usage? Also, is there a good way to get back to your app (like the login feature in social networking sites)?Varian
@Varian - Nothing is being hard alloc-ed here, so it's automatically set to autorelease.Wording
@Varian my guess would be no as I assume the 'webview' is launched in the safari application so it would fall under that processAtrocious
Hi is there any way I can open a link in safari web browser using javascript?? Like sending a link tag in my web view and opening a safari web browserCrenelate
Can anyone answer the title question? namely, how does one open safari from an app (on a device)? Just safari app, not a certain address within safari app. Calling @"http://" opens a new blank tab in safari app.Murage
Open url is useful because it returns NO if a parental restriction is turned on for Safari app. In one of my apps I found that one of my code paths introduced a bug by not handling this behaviour.Chute
dupe of earlier 5/9/09 answerGourmont
@Barett: Not exactly actually, because that's a 9/21/09 answerShulock
IMO the API call is similar enough that this answer would have been better applied as an edit or comment on the prior answer.Gourmont
U
53

UIApplication has a method called openURL:

example:

NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];

if (![[UIApplication sharedApplication] openURL:url]) {
  NSLog(@"%@%@",@"Failed to open url:",[url description]);
}
Unhopedfor answered 5/5, 2009 at 0:25 Comment(0)
H
16

you can open the url in safari with this:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com"]];
Henriquez answered 14/3, 2013 at 14:16 Comment(0)
R
5

With iOS 10 we have one different method with completion handler:

ObjectiveC:

NSDictionary *options = [NSDictionary new];
//options can be empty
NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];
[[UIApplication sharedApplication] openURL:url options:options completionHandler:^(BOOL success){
}];

Swift:

let url = URL(string: "http://www.stackoverflow.com")
UIApplication.shared.open(url, options: [:]) { (success) in
}
Rhondarhondda answered 21/9, 2016 at 20:35 Comment(0)
A
4

Maybe someone can use the Swift version:

In swift 2.2:

UIApplication.sharedApplication().openURL(NSURL(string: "https://www.google.com")!)

And 3.0:

UIApplication.shared().openURL(URL(string: "https://www.google.com")!)
Attah answered 28/6, 2016 at 17:59 Comment(0)
L
3

In swift 4 and 5, as OpenURL is depreciated, an easy way of doing it would be just

if let url = URL(string: "https://stackoverflow.com") {
    UIApplication.shared.open(url, options: [:]) 
}

You can also use SafariServices. Something like a Safari window within your app.

import SafariServices

...

if let url = URL(string: "https://stackoverflow.com") {
    let safariViewController = SFSafariViewController(url: url)        
    self.present(safariViewController, animated: true)
}

Lottie answered 13/4, 2018 at 15:10 Comment(1)
While this code snippet may solve the question, including an explanation helps to improve the quality of your response. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Conto
H
1

In Swift 3.0, you can use this class to help you communicate with. The framework maintainers have deprecated or removed previous answers.

import UIKit

class InterAppCommunication {
    static func openURI(_ URI: String) {
        UIApplication.shared.open(URL(string: URI)!, options: [:], completionHandler: { (succ: Bool) in print("Complete! Success? \(succ)") })
    }
}
Handsel answered 28/6, 2017 at 0:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.