在Safari中打开Swift链接

183

目前我正在使用 WebView 在我的应用程序中打开链接,但我正在寻找一种在 Safari 中打开链接的选项。

11个回答

412

虽然它不是 "嵌入到Swift" 中的一部分,但您可以使用标准的 UIKit 方法来完成它。请查看UIApplication的openUrl(_:)方法(已弃用)和open(_:options:completionHandler:)方法。

Swift 4 + Swift 5 (iOS 10及以上)

guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.open(url)

Swift 3(适用于 iOS 9 及以下版本)

guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.openURL(url)

Swift 2.2

guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.sharedApplication().openURL(url)    

如果我们添加类似这样的购买链接,是否有机会从应用商店获得批准? - Jan
9
在iOS 10.0中,你现在必须添加选项和处理程序: UIApplication.shared.open(URL(string:"http://www.google.com")!, options: [:], completionHandler: nil) - gabicuesta
1
@gabicuesta 实际上你不需要提供选项和完成处理程序,它们默认分别为[:]和nil。 - Jeremy
1
如果将“默认浏览器”设置为Google Chrome等其他浏览器,则iOS14不会在Safari中打开链接。 - Anonimys

95

从iOS 9及以上版本开始,您可以使用SFSafariViewController来向用户展示(请参见此处的文档)。基本上,您可以在不让用户离开您的应用程序的情况下获得将其发送到Safari的所有好处。要使用新的SFSafariViewController,只需:

import SafariServices

然后在事件处理程序中,以如下方式向用户呈现 Safari 视图控制器:

let svc = SFSafariViewController(url: url)
present(svc, animated: true, completion: nil)

Safari浏览器视图将会长成下面这样:

enter image description here


4
太棒了,谢谢!所有想在应用程序扩展中显示 Safari 浏览器的人都应该使用这个代码。在应用程序扩展中访问 sharedApplication 属性是被禁止的。更多信息请参见:https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionOverview.html#//apple_ref/doc/uid/TP40014214-CH2-SW2 - Baran Emre
1
优秀的解决方案 - Mutawe
2
有时候,苹果公司会因为应用程序使用旧的openURL方法而拒绝将其放入商店。现在,这已经不是首选解决方案了。 - disconnectionist

22

1
如何验证并避免重新打开已经打开的Safari应用程序。当启动时,我收到了一个警告/错误消息LAUNCH: Launch failure with -10652/ <FSNode 0x10d50cf90> { isDir = y, path = '/Applications/Safari.app' } - JeanNicolas

21

更新,适用于Swift 4:(感谢Marco Weber)

if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
     UIApplication.shared.openURL(requestUrl as URL) 
}

或者采用更加 Swift 风格的方式,使用 guard

guard let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") else {
    return
}

UIApplication.shared.openURL(requestUrl as URL) 

Swift 3:

您可以通过以下方式隐式地将NSURL检查为可选项:

if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
     UIApplication.sharedApplication().openURL(requestUrl)
}

4
Amit,不行,因为我已经明确解释了它,这是保证如果让requestUrl = ...,请求网址存在的方式。 - CodeOverRide
2
是的,有很多方法可以做事情。学习在某种情况下为什么应该使用特定的代码,而不是固执己见地说,“我是对的,所以你就错了”的心态。看起来你是新手程序员,这是我给你的建议,孩子。 - CodeOverRide
3
Amit:不,这行不通,你的观点是错误的。在Swift 2或1.2中是行不通的。也难怪,requestUrl不是可选类型,所以你不能使用!来解包它。 - Gusutafu
2
我更喜欢这种方法,而不是Mike S的方法,因为你在发送请求之前进行了nil检查。 - Nikolaj Simonsen
1
如果请求URL是:if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com"),则使用UIApplication.shared.openURL(requestUrl as URL)打开该URL。 - Marco Weber
显示剩余5条评论

12

Swift 3 & IOS 10.2

UIApplication.shared.open(URL(string: "http://www.stackoverflow.com")!, options: [:], completionHandler: nil)

Swift 3 和 IOS 10.2


但请注意,使用此版本将会停止您的应用在 iOS 9 及之前的版本上运行,除非您进行版本检查。 - CupawnTae

11

iOS 10 以后,您应该使用:

guard let url = URL(string: linkUrlString) else {
    return
}
    
if #available(iOS 10.0, *) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
    UIApplication.shared.openURL(url)
}

2
在 Swift 1.2 中:
@IBAction func openLink {    
    let pth = "http://www.google.com"
    if let url = NSURL(string: pth){
        UIApplication.sharedApplication().openURL(url)
}

1
在Swift 2.0中:
UIApplication.sharedApplication().openURL(NSURL(string: "http://stackoverflow.com")!)

1
如果您正在使用SwiftUI:
Link("Stack Overflow", destination: URL(string: "https://www.stackoverflow.com/")!)

1

Swift 5

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接