Checking permissions in Swift 2.0

1.3k Views Asked by At

No matter where I add code to check permissions for things like camera/mic/photos, the popup confirmation always kills my app or sends me back a few view controllers.

An example is as follows.

I have few view controllers in (part way through a registration process) when I have a page that deals with the permissions. Users tap a button to deal with the camera permission which uses the following code.

if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) !=  AVAuthorizationStatus.Authorized {
        AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted :Bool) -> Void in
            if granted == true {
                // do something
            } else {
               // determine whether not determined, denied etc and do something else
            }
        });
    }

However as soon as the iOS confirmation pops up, it throws the app back 2 view controllers. In other cases (e.g. on a viewDidLoad) permission requests kill the app as soon as the choice is made.

Any ideas what am I missing in my setup or how to prevent this behaviour?

Thanks.

1

There are 1 best solutions below

2
Misha On BEST ANSWER

I think you misunderstood my comment, what I did meant was

if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) !=  AVAuthorizationStatus.Authorized { // here you are checking if it's not authorized i..e  it's denied, NotDetermined or Restricted
   ....
    }
 else if if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) ==  AVAuthorizationStatus.Authorized
// do the something in case it's autorized

I'm listing keys here -

     <key>NSPhotoLibraryUsageDescription</key>
     <string>This app requires access to the photo library.</string>
     <key>NSCameraUsageDescription</key>
     <string>This app requires access to the camera.</string>