title: Scanning Barcode or QR code in Swift 3.0 using AVFoundation
body: <p>I am following this <a href="http://humberaquino.me/qrcode-scanning-in-swift/" rel="noreferrer"><strong>tutorial</strong></a> and tried to convert codes form Swift 2.0 to 3.0. But when I launched the application, the app doesn't work! I mean, nothing happens! Here is my code:</p>
  
  <p><strong>ViewController:</strong></p>
  
  <pre><code>class ViewController: UIViewController ,BarcodeDelegate {
  
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  
          let barcodeViewController: BarcodeViewController = segue.destination as! BarcodeViewController
          barcodeViewController.delegate = self
  
      }
  
  
  
      func barcodeReaded(barcode: String) {
          codeTextView.text = barcode
          print(barcode)
      }
  
  }
  </code></pre>
  
  <p><strong>BarcodeVC:</strong></p>
  
  <pre><code>import AVFoundation
  
  
  protocol BarcodeDelegate {
  
      func barcodeReaded(barcode: String)
  }
  
  class BarcodeViewController: UIViewController,AVCaptureMetadataOutputObjectsDelegate {
  
      var delegate: BarcodeDelegate?
      var captureSession: AVCaptureSession!
      var code: String?
  
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          // Do any additional setup after loading the view.
          print("works")
  
          self.captureSession = AVCaptureSession();
          let videoCaptureDevice: AVCaptureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
  
          do {
  
              let videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
  
              if self.captureSession.canAddInput(videoInput) {
                  self.captureSession.addInput(videoInput)
              } else {
                  print("Could not add video input")
              }
  
              let metadataOutput = AVCaptureMetadataOutput()
              if self.captureSession.canAddOutput(metadataOutput) {
                  self.captureSession.addOutput(metadataOutput)
  
                  metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
                  metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypePDF417Code]
              } else {
                  print("Could not add metadata output")
              }
  
              let previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
              previewLayer?.frame = self.view.layer.bounds
              self.view.layer .addSublayer(previewLayer!)
              self.captureSession.startRunning()
          } catch let error as NSError {
              print("Error while creating vide input device: \(error.localizedDescription)")
          }
  
  
  
      }
  
  
  
      //I THINK THIS METHOD NOT CALL !
      private func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
  
          // This is the delegate'smethod that is called when a code is readed
          for metadata in metadataObjects {
              let readableObject = metadata as! AVMetadataMachineReadableCodeObject
              let code = readableObject.stringValue
  
              // If the code is not empty the code is ready and we call out delegate to pass the code.
              if  code!.isEmpty {
                  print("is empty")
  
              }else {
  
                  self.captureSession.stopRunning()
                  self.dismiss(animated: true, completion: nil)
                  self.delegate?.barcodeReaded(barcode: code!)
  
  
              }
          }
  
      }
  </code></pre>
  
  <p>Here is the output:</p>
  
  <blockquote>
    <p><strong>2016-09-17 18:10:26.000919 BarcodeScaning[2610:674253] [MC]</strong> System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
    <strong>2016-09-17 18:10:26.007782 BarcodeScaning[2610:674253] [MC]</strong> Reading from public effective user settings.</p>
  </blockquote>