6

I was working on QR code scanner app on iOS where i was getting output AVCaptureOutput on the delegate method captureOutput:didOutputMetadataObjects:fromConnection:.

It was working perfectly on swift 3. After I've updated to xcode 9 and swift 4, it stopped working.

Arnab
  • 4,216
  • 2
  • 28
  • 50

2 Answers2

18

Okay I've found an update here.

Found that AVCaptureMetadataOutputObjectsDelegate method is changed

from

captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!)

to

metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection)

After changing this delegate method, its working good now.

Arnab
  • 4,216
  • 2
  • 28
  • 50
  • Note that the signature of this method changed (again..) after converting to Swift 4 - and it is not detected / fixed by the automatic migration - unlike other camera related callbacks. – GK100 Dec 04 '17 at 20:55
  • 1
    Thank you for this! What's bizarre is that the compiler did not complain that I was missing the protocol's function implementation because the function is marked `optional`. Considering this is the only method in this protocol to implement, I do not think it should be `optional`. – John Rogers Mar 13 '18 at 03:34
2

in Swift 4:

Replace

let metadataOutput = AVCaptureMetadataOutput()
metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
metadataOutput.metadataObjectTypes = metadataOutput.availableMetadataObjectTypes

With:

let metadataOutput = AVCaptureMetadataOutput()
metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
let supportedCodeTypes = [AVMetadataObject.ObjectType.upce,
                          AVMetadataObject.ObjectType.code39,
                          AVMetadataObject.ObjectType.code39Mod43,
                          AVMetadataObject.ObjectType.code93,
                          AVMetadataObject.ObjectType.code128,
                          AVMetadataObject.ObjectType.ean8,
                          AVMetadataObject.ObjectType.ean13,
                          AVMetadataObject.ObjectType.aztec,
                          AVMetadataObject.ObjectType.pdf417,
                          AVMetadataObject.ObjectType.qr]

metadataOutput.metadataObjectTypes = supportedCodeTypes
Nupur Sharma
  • 1,106
  • 13
  • 15