I had a quick ios swift programing question. Is it possible to trigger a segue from a QR code reading in swift? I looked it up on Google and found this app coda tutroial. I still don't understand how I would go about doing it with a segue. could someone explain how they did it in there app? Thanks!
UPDATE
thanks Christian Woerz for the useful tip!
I found out how to do it!
when this function:
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRectZero
messageLabel.text = "No QR code is detected"
return
}
// Get the metadata object.
let metadataObj = metadataObjects[0] as AVMetadataMachineReadableCodeObject
if metadataObj.type == AVMetadataObjectTypeQRCode {
// If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
let barCodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj as AVMetadataMachineReadableCodeObject) as AVMetadataMachineReadableCodeObject
qrCodeFrameView?.frame = barCodeObject.bounds;
if metadataObj.stringValue != nil {
messageLabel.text = metadataObj.stringValue
}
}
}
is fired do a performSegueWithIdentifier(_ identifier: String?, sender sender: AnyObject?)
inside the function like this
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRectZero
messageLabel.text = "No QR code is detected"
return
}
// Get the metadata object.
let metadataObj = metadataObjects[0] as AVMetadataMachineReadableCodeObject
if metadataObj.type == AVMetadataObjectTypeQRCode {
// If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
let barCodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj as AVMetadataMachineReadableCodeObject) as AVMetadataMachineReadableCodeObject
qrCodeFrameView?.frame = barCodeObject.bounds;
if metadataObj.stringValue != nil {
messageLabel.text = metadataObj.stringValue
}
}
**performSegueWithIdentifier(_ identifier: String?, sender sender: AnyObject?)**
}
and pass in the Identifier of the segue like
Identifier: thetransition
untested code but logic there :)
Yes it is possible.
But we can only give you a general answer, because there are many different ways and libraries to read a QR-code. But to give you a little example, you could use the QR code reader from this tutorial.
If you read a QR code with the code provided in this tutorial, a method will get called which receives the data of the QR code. In the tutorial it is captureOutput
.
Then in this method you can add code to perform a segue if the method is called.