Search code examples
iosswiftavcapturesessionios-camera

Stop AVCaptureSession when leaving ViewController


I have ViewController with AVCaptureSession. I can start and stop AVCaptureSession easily :

var captureSession: AVCaptureSession?
captureSession = AVCaptureSession()

//start
captureSession?.startRunning() 

//stop
captureSession?.stopRunning() 

A want to know how can I stop AVCaptureSession when user opens another ViewController.


Solution

  • Just have the viewWillDisappear method handle that for you.

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        captureSession?.stopRunning()
    }