Answers:
应该是以下内容:
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]);
}
您可以使用以下方法在野生动物园中打开网址:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com"]];
在iOS 10中,完成处理程序有一种不同的方法:
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){
}];
迅速:
let url = URL(string: "http://www.stackoverflow.com")
UIApplication.shared.open(url, options: [:]) { (success) in
}
也许有人可以使用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)
}