[ACCEPTED]-How can I launch Safari from an iPhone app?-launch
Accepted answer
should be the following :
NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(@"%@%@",@"Failed to open url:",[url description]);
}
0
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]);
}
0
you can open the url in safari with this:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com"]];
0
Maybe someone can use the Swift version:
In 1 swift 2.2:
UIApplication.sharedApplication().openURL(NSURL(string: "https://www.google.com")!)
And 3.0:
UIApplication.shared().openURL(URL(string: "https://www.google.com")!)
With iOS 10 we have one different method 1 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
}
In swift 4 and 5, as OpenURL is depreciated, an 3 easy way of doing it would be just
if let url = URL(string: "https://stackoverflow.com") {
UIApplication.shared.open(url, options: [:])
}
You can 2 also use SafariServices
. Something like a Safari window 1 within your app.
import SafariServices
...
if let url = URL(string: "https://stackoverflow.com") {
let safariViewController = SFSafariViewController(url: url)
self.present(safariViewController, animated: true)
}
In Swift 3.0, you can use this class to 3 help you communicate with. The framework 2 maintainers have deprecated or removed previous 1 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)") }) } }
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.