Safari中的Swift Open Link

Fab*_*gue 125 safari webview swift

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

Mik*_*e S 297

它并非"融入Swift",但您可以使用标准UIKit方法来实现它.看看UIApplicationopenUrl().

斯威夫特4

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

斯威夫特3

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

Swift 2.2

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

  • 在iOS 10.0中,您现在必须添加选项和处理程序:UIApplication.shared.open(URL(字符串:"http://www.google.com")!,选项:[:],completionHandler:nil) (8认同)
  • ```UIApplication.shared.openURL(URL(string:"http://www.stackoverflow.com")!)``` (2认同)

man*_*ito 61

使用iOS 9及更高版本的新功能,您可以向用户显示SFSafariViewController(请参阅此处的文档).基本上,您可以获得将用户发送到Safari而不会让他们离开您的应用程序的所有好处.要使用新的SFSafariViewController:

import SafariServices
Run Code Online (Sandbox Code Playgroud)

并且在事件处理程序中的某个位置向用户显示safari视图控制器,如下所示:

let svc = SFSafariViewController(url: url)
present(svc, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

safari视图将如下所示:

在此输入图像描述

  • 这太棒了.谢谢!想要在应用扩展程序中显示Safari浏览器的每个人都应该使用此代码.禁止访问app扩展中的`sharedApplication`属性.更多信息:https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionOverview.html#//apple_ref/doc/uid/TP40014214-CH2-SW2 (2认同)
  • Apple 有时会因为使用旧的 openURL 方法而拒绝商店中的应用程序。现在这应该是首选解决方案。 (2认同)

Cod*_*ide 19

更新为Swift 4 :(归功于Marco Weber)

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

或使用以下更快捷的风格guard:

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

UIApplication.shared.openURL(requestUrl as URL) 
Run Code Online (Sandbox Code Playgroud)

斯威夫特3:

您可以通过以下方式隐式检查NSURL是否为可选:

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

  • Amit,不,因为它已经明确地完成了,因为我已经解释过,如果让requestUrl = ...,保证requestUrl存在 (4认同)
  • 阿米特:不,它不起作用,你完全错了.在Swift 2或1.2中.难怪,requestUrl不是可选的,所以你不能用它打开它! (3认同)
  • 是的,有很多方法可以做.了解为什么你应该在某种情况下使用某些代码而不是一个顽固的小子说"我是对的,因此你错了"的心态.好像你是编程新手,这是我对你小孩的建议. (2认同)
  • 我比Mike S更喜欢这种方法,因为你在发送请求之前进行了nil检查. (2认同)

Kir*_*odi 15

斯威夫特 5

Swift 5:检查使用canOpneURL是否有效然后它是打开的。

guard let url = URL(string: "https://iosdevcenters.blogspot.com/") else {
     return
}

if UIApplication.shared.canOpenURL(url) {
     UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Run Code Online (Sandbox Code Playgroud)


ram*_*a n 12

Swift 3和IOS 10.2

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

Swift 3和IOS 10.2


Sam*_*ami 10

从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)
    }
Run Code Online (Sandbox Code Playgroud)