title: Swift Playground iPad cameras access
body: <p>In the last update notes for the Swift Playground app for the iPad I saw that the cameras would be accessible through the playgrounds.
  For testing purpose I've therefore created a playground to access my iPads back camera.
  As reference I've used those sources:</p>
  
  <ul>
  <li><strong><a href="https://github.com/codepath/ios_guides/wiki/Creating-a-Custom-Camera-View" rel="nofollow noreferrer">https://github.com/codepath/ios_guides/wiki/Creating-a-Custom-Camera-View</a></strong></li>
  <li><strong><a href="https://github.com/francip/swift-playground-camera/blob/master/Camera.playground/Contents.swift" rel="nofollow noreferrer">https://github.com/francip/swift-playground-camera/blob/master/Camera.playground/Contents.swift</a></strong></li>
  </ul>
  
  <p>Here is my code:</p>
  
  <pre><code>import UIKit
  import AVFoundation
  import PlaygroundSupport
  
  class MainViewController : UIViewController {
      private var _session: AVCaptureSession?
      private var _captureInput: AVCaptureInput?
      private var _stillImageOutput: AVCaptureStillImageOutput?
      private var _frontCamera: AVCaptureDevice?
      private var _previewView: UIView?
      private var _previewLayer: AVCaptureVideoPreviewLayer?
  
  public override func loadView() {
      print("loadView()")
  
      _previewView = UIView()
      guard let view = _previewView else {
          printError(errMsg: "View could not be created!")
          return
      }
      view.backgroundColor = UIColor.brown
      self.view = view
  }
  
  public override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
      // Create a capture session
      _session = AVCaptureSession()
      guard let session = _session  else {
          printError(errMsg: "Session is empty!")
          return
      }
      session.beginConfiguration()
      session.sessionPreset = AVCaptureSession.Preset.photo
  
      // Get access to front camera
      _frontCamera = AVCaptureDevice.default(for: AVMediaType.video)
  
      guard let frontCamera = _frontCamera
          else {
              printError(errMsg: "Front camera not accessible!")
              return
      }
  
      do{
          _captureInput = try AVCaptureDeviceInput(device: frontCamera)
      } catch let err as NSError {
          printError(errMsg: err.localizedDescription)
          return
      }
  
      // Add input to session
      guard let captureInput = _captureInput else {
          printError(errMsg: "Capture input not available!")
          return
      }
  
      if session.canAddInput(captureInput){
          session.addInput(captureInput)
      }
  
      // Configurw the image output
      _stillImageOutput = AVCaptureStillImageOutput()
      guard let sillImageOutput = _stillImageOutput else {
          printError(errMsg: "Image output not available!")
          return
      }
  
      sillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
  
      if session.canAddOutput(sillImageOutput) {
          session.addOutput(sillImageOutput)
      }
      session.commitConfiguration()
      // Add video preview layer
      _previewLayer = AVCaptureVideoPreviewLayer(session: session)
  
      guard let previewLayer = _previewLayer else {
          printError(errMsg: "Preview layer not available!")
          return
      }
  
      previewLayer.videoGravity = AVLayerVideoGravity.resizeAspect
      previewLayer.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
      guard let view = _previewView else {
          printError(errMsg: "View not available!")
          return
      }
      view.layer.addSublayer(previewLayer)
      view.layer.borderWidth = 5
  
      // Start the capture session
      session.startRunning()
  }
  
  public override func viewDidLoad() {
      super.viewDidLoad()
  }
  
  public override func viewDidAppear(_ animated: Bool) {
      super.viewDidAppear(animated)
      guard let previewLayer = _previewLayer else {
          printError(errMsg: "Preview layer not available!")
          return
      }
      guard let previewView = _previewView else {
          printError(errMsg: "View not available!")
          return
      }
      print(previewView.bounds)
      previewLayer.frame = previewView.bounds
  }
  
  private func printError(errMsg: String) {
      print("[Error]: " + errMsg)
  }
  }
  
  PlaygroundPage.current.liveView = MainViewController()
  </code></pre>
  
  <p>Please ignore the private variables for the session, etc. I know that I can/should make them method local.</p>
  
  <p>The only thing I see is the brown background of the view. 
  <a href="https://i.stack.imgur.com/bIJtc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bIJtc.png" alt="enter image description here"></a>
  When running the app for the first time iOS asked me if the Playgrounds app should have access to the camera which I accepted. In the settings I can also see that the app has access to the camera.</p>
  
  <p>iOS Version: 10.3.3
  Playground App: 1.6.1</p>