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

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

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

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


当前回答

SWIFT 3.0

               if let url = URL(string: "https://www.google.com") {
                 UIApplication.shared.open(url, options: [:])
               }

其他回答

因为这个答案从iOS 10.0开始就被弃用了,所以更好的答案是:

if #available(iOS 10.0, *) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}else{
    UIApplication.shared.openURL(url)
}

y en Objective-c

[[UIApplication sharedApplication] openURL:@"url string" options:@{} completionHandler:^(BOOL success) {
        if (success) {
            NSLog(@"Opened url");
        }
    }];

并且,如果你不确定提供的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];

看看UIApplication上的-openURL:方法。它应该允许你向系统传递一个NSURL实例,系统将决定在哪个应用程序中打开它并启动该应用程序。(请记住,你可能想要检查- canopenurl:首先,以防URL不能被当前安装在系统上的应用程序处理-尽管这对于普通的http://链接可能不是问题。)

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

不要忘记导入SafariServices

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

未弃用的Objective-C版本如下:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://apple.com"] options:@{} completionHandler:nil];