在设置页面,我想包括三个链接到
我的应用支持网站 YouTube应用教程 我的主要网站(例如:链接到“由Dale Dietrich创建”标签。)
我搜索了这个网站和网页还有我的文档我没有发现任何明显的东西。
注意:我不想在我的应用程序中打开网页。我只想将链接发送到Safari,该链接在那里打开。我已经看到许多应用程序在他们的设置页面做同样的事情,所以这必须是可能的。
在设置页面,我想包括三个链接到
我的应用支持网站 YouTube应用教程 我的主要网站(例如:链接到“由Dale Dietrich创建”标签。)
我搜索了这个网站和网页还有我的文档我没有发现任何明显的东西。
注意:我不想在我的应用程序中打开网页。我只想将链接发送到Safari,该链接在那里打开。我已经看到许多应用程序在他们的设置页面做同样的事情,所以这必须是可能的。
当前回答
在Swift 1.2中,试试这个:
let pth = "http://www.google.com"
if let url = NSURL(string: pth){
UIApplication.sharedApplication().openURL(url)
其他回答
看看UIApplication上的-openURL:方法。它应该允许你向系统传递一个NSURL实例,系统将决定在哪个应用程序中打开它并启动该应用程序。(请记住,你可能想要检查- canopenurl:首先,以防URL不能被当前安装在系统上的应用程序处理-尽管这对于普通的http://链接可能不是问题。)
斯威夫特3.0
if let url = URL(string: "https://www.reddit.com") {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:])
} else {
UIApplication.shared.openURL(url)
}
}
这也支持运行旧版本iOS的设备
这里需要检查将要打开的url是否能够通过设备或模拟器打开。因为有些时候(大多数在模拟器),我发现它会导致崩溃。
objective - c
NSURL *url = [NSURL URLWithString:@"some url"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
斯威夫特2.0
let url : NSURL = NSURL(string: "some url")!
if UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.sharedApplication().openURL(url)
}
斯威夫特4.2
guard let url = URL(string: "some url") else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
试试这个:
NSString *URL = @"xyz.com";
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:URL]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:URL]];
}
SWIFT 3.0
if let url = URL(string: "https://www.google.com") {
UIApplication.shared.open(url, options: [:])
}