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

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

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

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


当前回答

SWIFT 3.0

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

其他回答

在Swift 1.2中,试试这个:

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

以下是我所做的:

我在头文件.h中创建了一个IBAction,如下所示: ——(IBAction) openDaleDietrichDotCom: (id)发送者; 我在设置页面上添加了一个UIButton,其中包含我想要链接的文本。 我将按钮连接到文件所有者中的IBAction。 然后执行以下操作:

objective - c

- (IBAction)openDaleDietrichDotCom:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.daledietrich.com"]];
}

斯威夫特

(IBAction在viewController中,而不是头文件中)

if let link = URL(string: "https://yoursite.com") {
  UIApplication.shared.open(link)
}

注意,我们不需要转义字符串和/或地址,比如: 让myNormalString = "https://example.com"; 让myEscapedString = myNormalString。addingPercentEncoding (withAllowedCharacters: .urlHostAllowed) ! 事实上,逃避可能会导致开放失败。

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

SWIFT 3.0

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

试试这个:

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