This might be a rather obvious question, but can you launch the Safari browser from an iPhone app?
Asked
Active
Viewed 8.5k times
7 Answers
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]);
}

Intrications
- 16,782
- 9
- 50
- 50

surtyaar
- 2,532
- 1
- 18
- 13
-
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)? – brendan Mar 09 '12 at 19:25
-
@brendan - Nothing is being hard alloc-ed here, so it's automatically set to autorelease. – iveytron Oct 08 '12 at 14:54
-
1@brendan my guess would be no as I assume the 'webview' is launched in the safari application so it would fall under that process – surtyaar Jul 24 '13 at 20:41
-
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 browser – user1010819 Dec 12 '13 at 16:58
-
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. – mircaea Apr 08 '15 at 18:43
-
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. – StackRunner May 01 '15 at 09:58
-
12dupe of earlier 5/9/09 answer – Barett Aug 08 '15 at 15:49
-
2@Barett: Not exactly actually, because [that's a 9/21/09 answer](http://stackoverflow.com/posts/822764/revisions#spacer-2140e39e-53ae-42a0-8e53-9fc38b6e2731) – Bergi Aug 09 '15 at 17:00
-
4IMO the API call is similar enough that this answer would have been better applied as an edit or comment on the prior answer. – Barett Aug 09 '15 at 18:33
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]);
}

Intrications
- 16,782
- 9
- 50
- 50

Brad The App Guy
- 16,255
- 2
- 41
- 60
16
you can open the url in safari with this:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com"]];

Rahul Patel
- 5,858
- 6
- 46
- 72

Dhaval Parmar
- 293
- 3
- 5
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")!)

Jens Peter
- 715
- 6
- 10
4
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
}

DZoki019
- 382
- 2
- 13
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)
}

Jia Chen
- 43
- 7
-
While this code snippet may solve the question, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) 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. – Stefan Crain Apr 13 '18 at 15:34
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)") }) } }

lisp-ceo
- 119
- 1
- 4