Thecoder
Thecoder

Reputation: 63

Scan QR Code and open URL – Swift AVFoundation library

Sorry, this is my very first iOS app! I am trying to implement a very simple QR code scanner to the app using QRCodeReader.swift but I have no idea how to handle the URL from the result. It is supposed to redirect instantly to the link but is not working. Thank you!

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


}
}

Upvotes: 0

Views: 1887

Answers (1)

Rajamohan S
Rajamohan S

Reputation: 7269

Swift 3.0

Optional Chaining: Optional Chaining as an Alternative to Forced Unwrapping.

You can check scanned String is url,xml,JSON by optional chaining. For more go to apple document 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
}

Upvotes: 1

Related Questions