Custom Camera view not appearing on swift playgrounds

Hi! I am using a camera with swift playgrounds on iPad, but it is not showing up,

here is the code

import AVFoundation

var preview: AVCaptureVideoPreviewLayer?

override func viewWillAppear(_ animated: Bool) {

super.viewWillAppear(animated)

// create a capture session for the camera input

let captureSession = AVCaptureSession()

captureSession.sessionPreset = AVCaptureSessionPresetPhoto

// Choose the back camera as input device

let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

var error: NSError?

var input: AVCaptureDeviceInput!

do {

input = try AVCaptureDeviceInput(device: backCamera)

} catch let cameraError as NSError {

error = cameraError

input = nil

}

// check if the camera input is available

if error == nil && captureSession.canAddInput(input) {

// ad camera input to the capture session

captureSession.addInput(input)

let photoImageOutput = AVCapturePhotoOutput()

// Create an UIlayer with the capture session output

photoImageOutput.photoSettingsForSceneMonitoring = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecJPEG])

if captureSession.canAddOutput(photoImageOutput) {

captureSession.addOutput(photoImageOutput)

preview = AVCaptureVideoPreviewLayer(session: captureSession)

preview?.videoGravity = AVLayerVideoGravityResizeAspectFill

preview?.connection.videoOrientation = AVCaptureVideoOrientation.portrait

preview?.frame = cameraView.frame

cameraView.layer.addSublayer(preview!)

captureSession.startRunning()

}

}

What did I Miss?