ios 如何从 iPhone 应用程序启动 Safari?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/822599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I launch Safari from an iPhone app?
提问by keuminotti
This might be a rather obvious question, but can you launch the Safari browser from an iPhone app?
这可能是一个相当明显的问题,但是您可以从 iPhone 应用程序启动 Safari 浏览器吗?
回答by surtyaar
should be the following :
应该如下:
NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(@"%@%@",@"Failed to open url:",[url description]);
}
回答by Brad The App Guy
UIApplication has a method called openURL:
UIApplication 有一个方法叫做 openURL:
example:
例子:
NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(@"%@%@",@"Failed to open url:",[url description]);
}
回答by Dhaval Parmar
you can open the url in safari with this:
您可以使用以下方法在 safari 中打开网址:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com"]];
回答by DZoki019
With iOS 10 we have one different method with completion handler:
在 iOS 10 中,我们有一种不同的带有完成处理程序的方法:
ObjectiveC:
目标C:
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
}
回答by Jia Chen
In swift 4 and 5, as OpenURL is depreciated, an easy way of doing it would be just
在 swift 4 和 5 中,由于 OpenURL 已贬值,一个简单的方法就是
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.
您也可以使用SafariServices
. 类似于应用程序中的 Safari 窗口。
import SafariServices
...
if let url = URL(string: "https://stackoverflow.com") {
let safariViewController = SFSafariViewController(url: url)
self.present(safariViewController, animated: true)
}
回答by Jens Peter
Maybe someone can use the Swift version:
也许有人可以使用 Swift 版本:
In swift 2.2:
在 swift 2.2 中:
UIApplication.sharedApplication().openURL(NSURL(string: "https://www.google.com")!)
And 3.0:
和 3.0:
UIApplication.shared().openURL(URL(string: "https://www.google.com")!)
回答by 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.
在 Swift 3.0 中,您可以使用这个类来帮助您进行交流。框架维护者已弃用或删除了以前的答案。
import UIKit class InterAppCommunication { static func openURI(_ URI: String) { UIApplication.shared.open(URL(string: URI)!, options: [:], completionHandler: { (succ: Bool) in print("Complete! Success? \(succ)") }) } }