在设置页面,我想包括三个链接到

我的应用支持网站 YouTube应用教程 我的主要网站(例如:链接到“由Dale Dietrich创建”标签。)

我搜索了这个网站和网页还有我的文档我没有发现任何明显的东西。

注意:我不想在我的应用程序中打开网页。我只想将链接发送到Safari,该链接在那里打开。我已经看到许多应用程序在他们的设置页面做同样的事情,所以这必须是可能的。


当前回答

斯威夫特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的设备

其他回答

在Swift 1.2中,试试这个:

let pth = "http://www.google.com"
    if let url = NSURL(string: pth){
        UIApplication.sharedApplication().openURL(url)

斯威夫特3 带有Done按钮的解决方案

不要忘记导入SafariServices

if let url = URL(string: "http://www.yoururl.com/") {
            let vc = SFSafariViewController(url: url, entersReaderIfAvailable: true)
            present(vc, animated: true)
        }

试试这个:

NSString *URL = @"xyz.com";
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:URL]])
{
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:URL]];
}

openURL(:)在iOS 10.0中已弃用,相反,你应该在UIApplication上使用以下实例方法:

Swift使用示例 这将在Safari中打开“https://apple.com”。

if let url = URL(string: "https://apple.com") {
    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
}

https://developer.apple.com/reference/uikit/uiapplication/1648685-open

并且,如果你不确定提供的URL文本是否有一个方案:

NSString* text = @"www.apple.com";
NSURL*    url  = [[NSURL alloc] initWithString:text];

if (url.scheme.length == 0)
{
    text = [@"http://" stringByAppendingString:text];
    url  = [[NSURL alloc] initWithString:text];
}

[[UIApplication sharedApplication] openURL:url];