D. Finna D. Finna - 1 month ago 16x
iOS Question

Making QR Code reader start again when stopped swift

I have a QR code reader that when finding a QR code is opening up my detailViewController in a modal. On my detailViewController you are able to dismiss the modal, for instance if it is not the right QR code you wanted to scan. The problem is that in my code the QR code reader is set to stop, when the QR code is found and when you dismiss the modal, it does not start up again? How can I make it start up again?

Here you have my code from the QR code viewcontroller, just the part that does something to the rest of my app when finding a QR code and at the bottom stopping the QR reader:

if metadataObj.stringValue != nil {
// messageLabel.text = metadataObj.stringValue

messageLabel.backgroundColor = UIColor(red: 46/255.0, green: 204/255.0, blue: 113/255.0, alpha: 1.0)
messageLabel.text = "QR code found!"
passData = metadataObj.stringValue as String

dispatch_async(dispatch_get_main_queue()) { [unowned self] in
self.performSegueWithIdentifier("SendDataSegue", sender: self.passData)

self.captureSession!.stopRunning() // This is the part stopping it


That is stopping because you are stopping the captureSession

self.captureSession!.stopRunning() // This is the part stopping it

After dismissing your ViewController you have to check whether your self.captureSession is nil or not, if it is nil then you have to create a new capture session and if it is not nil then you have to start that session again.Like this


So in viewDidLoad initialize your captureSession and in viewWillAppear start your session.As on Dismissing the ViewController, viewDidLoad will not called.