title: I want to add an ean 13 barcode scanner to my application but it wont work on swift playgrounds for Ipad
body: <p>I am trying to add an scanner to my application but everytime when I run the application it sais that there is a problem in my code and that I have to try again
  The code what I have is from <a href="https://www.hackingwithswift.com/example-code/media/how-to-scan-a-barcode" rel="nofollow noreferrer">https://www.hackingwithswift.com/example-code/media/how-to-scan-a-barcode</a>, I tried to paste it on to an blank document and a Xcode-playground document but both of them didn’t work.<a href="https://i.stack.imgur.com/ma5S5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ma5S5.png" alt="This is the response after running the code Down bellow" /></a></p>
  <pre><code>Import AVFoundation
  import UIKit
  
  class ScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
      var captureSession: AVCaptureSession!
      var previewLayer: AVCaptureVideoPreviewLayer!
  override func viewDidLoad() {
      super.viewDidLoad()
  
      view.backgroundColor = UIColor.black
      captureSession = AVCaptureSession()
  
      guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
      let videoInput: AVCaptureDeviceInput
  
      do {
          videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
      } catch {
          return
      }
  
      if (captureSession.canAddInput(videoInput)) {
          captureSession.addInput(videoInput)
      } else {
          failed()
          return
      }
  
      let metadataOutput = AVCaptureMetadataOutput()
  
      if (captureSession.canAddOutput(metadataOutput)) {
          captureSession.addOutput(metadataOutput)
  
          metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
          metadataOutput.metadataObjectTypes = [.ean8, .ean13, .pdf417]
      } else {
          failed()
          return
      }
  
      previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
      previewLayer.frame = view.layer.bounds
      previewLayer.videoGravity = .resizeAspectFill
      view.layer.addSublayer(previewLayer)
  
      captureSession.startRunning()
  }
  
  func failed() {
      let ac = UIAlertController(title: &quot;Scanning not supported&quot;, message: &quot;Your device does not support scanning a code from an item. Please use a device with a camera.&quot;, preferredStyle: .alert)
      ac.addAction(UIAlertAction(title: &quot;OK&quot;, style: .default))
      present(ac, animated: true)
      captureSession = nil
  }
  
  override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
  
      if (captureSession?.isRunning == false) {
          captureSession.startRunning()
      }
  }
  
  override func viewWillDisappear(_ animated: Bool) {
      super.viewWillDisappear(animated)
  
      if (captureSession?.isRunning == true) {
          captureSession.stopRunning()
      }
  }
  
  func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
      captureSession.stopRunning()
  
      if let metadataObject = metadataObjects.first {
          guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
          guard let stringValue = readableObject.stringValue else { return }
          AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
          found(code: stringValue)
      }
  
      dismiss(animated: true)
  }
  
  func found(code: String) {
      print(code)
  }
  
  override var prefersStatusBarHidden: Bool {
      return true
  }
  
  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
      return .portrait
  }
  </code></pre>
  <p>}</p>