应该如下:
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"]];
也许有人可以使用 Swift 版本:
在 Swift 2.2 中:
UIApplication.sharedApplication().openURL(NSURL(string: "https://www.google.com")!)
和 3.0:
UIApplication.shared().openURL(URL(string: "https://www.google.com")!)
在 iOS 10 中,我们有一种不同的完成处理程序方法:
目标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 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 中,你可以使用这个类来帮助你进行交流。框架维护者已弃用或删除了以前的答案。
import UIKit class InterAppCommunication { static func openURI(_ URI: String) { UIApplication.shared.open(URL(string: URI)!, options: [:], completionHandler: { (succ: Bool) in print("Complete! Success? \(succ)") }) } }
不定期副业成功案例分享