gpt4 book ai didi

ios - openURL 和 canOpenURL 之间的区别

转载 作者:可可西里 更新时间:2023-10-31 23:57:29 54 4
gpt4 key购买 nike

我需要在 safari 浏览器中打开一个链接,但我有疑问,我应该使用哪种方法? openURL/opencanOpenURL。谁能帮我解释一下这两种功能之间的实际区别是什么?

 if #available(iOS 10.0, *) {
UIApplication.shared.open(URL(string: urlStr)!, options: [:], completionHandler: nil)
UIApplication.shared.canOpenURL(URL(string: urlStr)!)

} else {
UIApplication.shared.openURL(URL(string: urlStr)!) //introduced: 2.0, deprecated: 10.0,

UIApplication.shared.canOpenURL(URL(string: urlStr)!) // available(iOS 3.0, *)
}

最佳答案

canOpenURL(_:)

返回一个 bool 值,指示 URL 的方案是否可以由安装在设备上的某些应用程序处理。

openURL(_:)

尝试在指定的 URL 打开资源。

openURL(_:) 已弃用 - iOS 10.0

改用open(_:options:completionHandler:) 方法。示例:

if UIApplication.shared.canOpenURL(url) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: { (success) in

})
} else {
UIApplication.shared.openURL(url)
}
}

If your app is linked on or after iOS 9.0, you must declare the URL schemes you pass to this method by adding the LSApplicationQueriesSchemes key to your app's Info.plist file. This method always returns false for undeclared schemes, whether or not an appropriate app is installed.

关于ios - openURL 和 canOpenURL 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48048651/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com