【发布时间】:2010-10-23 18:47:43
【问题描述】:
这可能是一个相当明显的问题,但是您可以从 iPhone 应用程序启动 Safari 浏览器吗?
【问题讨论】:
这可能是一个相当明显的问题,但是您可以从 iPhone 应用程序启动 Safari 浏览器吗?
【问题讨论】:
应该如下:
NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(@"%@%@",@"Failed to open url:",[url description]);
}
【讨论】:
UIApplication 有一个名为 openURL 的方法:
示例:
NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(@"%@%@",@"Failed to open url:",[url description]);
}
【讨论】:
你可以在 safari 中打开网址:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com"]];
【讨论】:
在 iOS 10 中,我们有一种不同的 completion handler 方法:
目标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){
}];
斯威夫特:
let url = URL(string: "http://www.stackoverflow.com")
UIApplication.shared.open(url, options: [:]) { (success) in
}
【讨论】:
也许有人可以使用 Swift 版本:
在 Swift 2.2 中:
UIApplication.sharedApplication().openURL(NSURL(string: "https://www.google.com")!)
和 3.0:
UIApplication.shared().openURL(URL(string: "https://www.google.com")!)
【讨论】:
在 swift 4 和 5 中,随着 OpenURL 的贬值,一个简单的方法就是
if let url = URL(string: "https://stackoverflow.com") {
UIApplication.shared.open(url, options: [:])
}
您也可以使用SafariServices。类似于应用中的 Safari 窗口。
import SafariServices
...
if let url = URL(string: "https://stackoverflow.com") {
let safariViewController = SFSafariViewController(url: url)
self.present(safariViewController, animated: true)
}
【讨论】:
在 Swift 3.0 中,你可以使用这个类来帮助你进行交流。框架维护者已弃用或删除了以前的答案。
导入 UIKit
类 InterAppCommunication {
静态函数 openURI(_ URI: String) {
UIApplication.shared.open(URL(string: URI)!, options: [:], completionHandler: { (succ: Bool) in print("Complete! Success? \(succ)") })
}
}
【讨论】: