ChatGPT解决这个技术问题 Extra ChatGPT

How can I launch Safari from an iPhone app?

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


I
Intrications

should be the following :

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

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

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 my guess would be no as I assume the 'webview' is launched in the safari application so it would fall under that process
dupe of earlier 5/9/09 answer
@Barett: Not exactly actually, because that's a 9/21/09 answer
IMO the API call is similar enough that this answer would have been better applied as an edit or comment on the prior answer.
I
Intrications

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

R
Rahul Patel

you can open the url in safari with this:

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

J
Jens Peter

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

D
DZoki019

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
}

J
Jia Chen

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


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.
l
lisp-ceo

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