【问题标题】:Scan QR Code and open URL – Swift AVFoundation library扫描二维码并打开 URL – Swift AVFoundation 库
【发布时间】:2017-06-29 01:02:37
【问题描述】:

抱歉,这是我的第一个 iOS 应用!我正在尝试使用 QRCodeReader.swift 为应用程序实现一个非常简单的 QR 码扫描器,但我不知道如何处理结果中的 URL。它应该立即重定向到链接,但不起作用。谢谢!

func reader(_ reader: QRCodeReaderViewController, didScanResult result: QRCodeReaderResult) {
reader.stopScanning()


dismiss(animated: true) { [weak self] in


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


}
}

【问题讨论】:

  • 你打开系统设置有没有发现这个问题?
  • 还没有!它会扫描二维码,但不会转到链接。
  • 那么这个链接是什么?因为我使用了你的代码并且它有效。
  • 应该用任意url打开默认浏览器

标签: ios swift xcode cocoa swift3


【解决方案1】:

Swift 3.0

Optional Chaining: 可选链接作为强制展开的替代方案。

您可以通过optional chaining 检查扫描的Stringurl,xml,JSON。更多内容请到苹果文档here

if let url = URL(string: result.value){ //check whether the string is URL
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.openURL(url)
    }
}else {
    //do more if scanned text is not url string
}

【讨论】:

  • 是否可以先检查字符串是否为 URL?谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-21
  • 2017-12-22
  • 1970-01-01
  • 2021-12-16
  • 1970-01-01
相关资源
最近更新 更多