How can I launch Safari from an iPhone app?

IosSafariLaunch

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

should be the following :

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

if (![[UIApplication sharedApplication] openURL:url]) {
    NSLog(@"%@%@",@"Failed to open url:",[url description]);
}

Solution 2 - Ios

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]);
}

Solution 3 - Ios

you can open the url in safari with this:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com"]];

Solution 4 - Ios

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
}

Solution 5 - Ios

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")!)

Solution 6 - Ios

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)
}

Solution 7 - Ios

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)") }) } }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkeuminottiView Question on Stackoverflow
Solution 1 - IossurtyaarView Answer on Stackoverflow
Solution 2 - IosBrad The App GuyView Answer on Stackoverflow
Solution 3 - IosDhaval ParmarView Answer on Stackoverflow
Solution 4 - IosDZoki019View Answer on Stackoverflow
Solution 5 - IosJens PeterView Answer on Stackoverflow
Solution 6 - IosJia ChenView Answer on Stackoverflow
Solution 7 - Ioslisp-ceoView Answer on Stackoverflow