ChatGPT解决这个技术问题 Extra ChatGPT

在 Safari 中快速打开链接

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


J
Josh Correia

它不是“融入 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)

斯威夫特 2.2

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

如果我们添加一些这样的购买 URL,应用商店是否有机会?
在 iOS 10.0 中,您现在必须添加选项和处理程序: UIApplication.shared.open(URL(string:"google.com")!, options: [:], completionHandler: nil)
@gabicuesta您实际上不必提供选项和completionHandler,它们分别默认为[:]和nil
如果将“默认浏览器”设置为其他,如 Google Chrome 等,iOS14 不会在 Safari 中打开链接。
m
manncito

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

import SafariServices

并且在事件处理程序的某处向用户呈现 Safari 视图控制器,如下所示:

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

Safari 视图将如下所示:

https://i.stack.imgur.com/6pCEl.png


这太棒了。谢谢!每个想要在应用程序扩展中显示 Safari 浏览器的人都应该使用此代码。禁止访问应用扩展中的 sharedApplication 属性。更多信息:developer.apple.com/library/archive/documentation/General/…
出色的解决方案
Apple 有时会拒绝商店中使用旧的 openURL 方法的应用程序。这现在应该是首选的解决方案。
C
CodeOverRide

为 Swift 4 更新:(感谢 Marco Weber)

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

或者使用 guard 使用更多的 swift 风格:

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

UIApplication.shared.openURL(requestUrl as URL) 

斯威夫特 3:

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

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

阿米特,不,因为它是明确完成的,正如我已经解释的那样,如果让 requestUrl = ... 保证 requestUrl 存在
是的,有很多方法可以做事。了解为什么你应该在某种情况下使用某些代码,而不是成为一个固执的小子,说“我是对的,所以你的错”的心态。好像你是编程新手,这是我给你的孩子的建议。
阿米特:不,它不起作用,你完全错了。在 Swift 2 或 1.2 中。难怪, requestUrl 不是可选的,所以你不能用 ! 解开它。
我比 Mike S 的方法更喜欢这种方法,因为您在发送请求之前会进行 nil 检查。
为 Swift4 更新:if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") { UIApplication.shared.openURL(requestUrl as URL) }
O
Olcay Ertaş

斯威夫特 5

Swift 5: Check using canOpneURL if valid then it's open.

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

if UIApplication.shared.canOpenURL(url) {
     UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

如何验证并避免重新打开已打开的应用程序 Safari。启动时出现警告/错误 LAUNCH: Launch failure with -10652/ <FSNode 0x10d50cf90> { isDir = y, path = '/Applications/Safari.app' }
r
ramchandra n

斯威夫特 3 和 IOS 10.2

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

斯威夫特 3 和 IOS 10.2


但请注意,使用此版本将停止您的应用程序在 iOS 9 及更早版本上运行,除非您对其进行版本检查
O
Olcay Ertaş

从 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)
}

C
Chris K

在 Swift 1.2 中:

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

K
Kaptain

在 Swift 2.0 中:

UIApplication.sharedApplication().openURL(NSURL(string: "http://stackoverflow.com")!)

d
dqualias

如果您使用 SwiftUI:

Link("Stack Overflow", destination: URL(string: "https://www.stackoverflow.com/")!)

O
Olcay Ertaş

IOS 11.2 斯威夫特 3.1- 4

let webView = WKWebView()

override func viewDidLoad() {
    super.viewDidLoad()
    guard let url = URL(string: "https://www.google.com") else { return }
    webView.frame = view.bounds
    webView.navigationDelegate = self
    webView.load(URLRequest(url: url))
    webView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
    view.addSubview(webView)
}

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if navigationAction.navigationType == .linkActivated  {
        if let url = navigationAction.request.url,
            let host = url.host, !host.hasPrefix("www.google.com"),
            UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url)
            print(url)
            print("Redirected to browser. No need to open it locally")
            decisionHandler(.cancel)
        } else {
            print("Open it locally")
            decisionHandler(.allow)
        }
    } else {
        print("not a user click")
        decisionHandler(.allow)
    }
}

s
sohil

斯威夫特 5

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