cb428
cb428

Reputation: 495

Toggling camera permissions causes crash

I'm trying to handle asking a user for permission to access the camera. So far, I have a method that properly returns the current Authorization status for the camera, and when the camera permission is Denied the app works fine.

However, if I go into my phone settings and toggle the camera permission to "Allow", I keep getting this weird crash when I restart the app and try to start the capture session (my camera launches immediately when the app opens FYI). Here's the crash:

* Terminating app due to uncaught exception 'NSGenericException', reason: '* AVCaptureSession can't startRunning between calls to beginConfiguration / commitConfiguration'

Here is the code to start the session (which is after I commitConfiguration):

dispatch_async(captureSessionQueue) {
        self.captureSession.startRunning()
    }

The above code is called after I check the Authorization status by calling this function. The cameraView with the capture session is only initialized after I check that permission is Authorized:

public func IsCameraAuthorized() -> AVAuthorizationStatus {
var authorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
switch authorizationStatus {
case .NotDetermined:
    // permission dialog not yet presented, request authorization
    AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo) { granted in
        if granted {
            authorizationStatus = .Authorized
        } else {
            // user denied
            authorizationStatus = .Denied
        }
    }
    break
case .Authorized:
    // go
    break
case .Denied, .Restricted:
    // the user explicitly denied camera usage or is not allowed to access the camera devices
    break
}

return authorizationStatus

}

Any thoughts on what I am doing wrong? Why doesn't this work if I commitConfiguration before starting the session?

Upvotes: 0

Views: 776

Answers (1)

shinoys222
shinoys222

Reputation: 414

Nothing's wrong with your authorization code. Try removing your dispatch_async block and start capturing inside viewDidAppear

Upvotes: 0

Related Questions