I am using this code, but unfortunately it doesn't work.
After a user has denied camera access, I want to ask them for permission to use the camera again the next time they try to load it (in this case it's a barcode scanner using the camera view). I always get AVAuthorizationStatusDenied
and then granted
always automatically returns NO
even though I ask for it again in code.
Many of my users are e-mailing me saying "my screen is black when I try to barcode scan" and it's because they have denied camera access for some reason. I want to be able to prompt them again because most likely the denial was a mistake.
Is there a possible way to do this?
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if(authStatus == AVAuthorizationStatusAuthorized) { NSLog(@"%@", @"You have camera access"); } else if(authStatus == AVAuthorizationStatusDenied) { NSLog(@"%@", @"Denied camera access"); [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { if(granted){ NSLog(@"Granted access to %@", AVMediaTypeVideo); } else { NSLog(@"Not granted access to %@", AVMediaTypeVideo); } }]; } else if(authStatus == AVAuthorizationStatusRestricted) { NSLog(@"%@", @"Restricted, normally won't happen"); } else if(authStatus == AVAuthorizationStatusNotDetermined) { NSLog(@"%@", @"Camera access not determined. Ask for permission."); [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { if(granted){ NSLog(@"Granted access to %@", AVMediaTypeVideo); } else { NSLog(@"Not granted access to %@", AVMediaTypeVideo); } }]; } else { NSLog(@"%@", @"Camera access unknown error."); }
After some research it looks like you can't do what I'd like. Here is the alternative that I coded to pop a dialog and open the Settings app automatically if on iOS 8+.
Some notes:
NSCameraUsageDescription
key in your Info.plist to be able ask for camera access, otherwise your app will crash at runtime.Swift 5.2:
At the top of your view controller:
import AVFoundation
Before opening the camera view:
@IBAction func goToCamera() { let status = AVCaptureDevice.authorizationStatus(for: .video) switch (status) { case .authorized: self.popCamera() case .notDetermined: AVCaptureDevice.requestAccess(for: .video) { (granted) in if (granted) { self.popCamera() } else { self.camDenied() } } case .denied: self.camDenied() case .restricted: let alert = UIAlertController(title: "Restricted", message: "You've been restricted from using the camera on this device. Without camera access this feature won't work. Please contact the device owner so they can give you access.", preferredStyle: .alert) let okAction = UIAlertAction(title: "OK", style: .default, handler: nil) alert.addAction(okAction) self.present(alert, animated: true, completion: nil) @unknown default: fatalError() } }
Denial alert with completion block:
func camDenied() { DispatchQueue.main.async { var alertText = "It looks like your privacy settings are preventing us from accessing your camera to do barcode scanning. You can fix this by doing the following:\n\n1. Close this app.\n\n2. Open the Settings app.\n\n3. Scroll to the bottom and select this app in the list.\n\n4. Turn the Camera on.\n\n5. Open this app and try again." var alertButton = "OK" var goAction = UIAlertAction(title: alertButton, style: .default, handler: nil) if UIApplication.shared.canOpenURL(URL(string: UIApplication.openSettingsURLString)!) { alertText = "It looks like your privacy settings are preventing us from accessing your camera to do barcode scanning. You can fix this by doing the following:\n\n1. Touch the Go button below to open the Settings app.\n\n2. Turn the Camera on.\n\n3. Open this app and try again." alertButton = "Go" goAction = UIAlertAction(title: alertButton, style: .default, handler: {(alert: UIAlertAction!) -> Void in UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil) }) } let alert = UIAlertController(title: "Error", message: alertText, preferredStyle: .alert) alert.addAction(goAction) self.present(alert, animated: true, completion: nil) } }
Objective-C:
At the top of your view controller:
#import <AVFoundation/AVFoundation.h>
Before opening the camera view:
- (IBAction)goToCamera { AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if(authStatus == AVAuthorizationStatusAuthorized) { [self popCamera]; } else if(authStatus == AVAuthorizationStatusNotDetermined) { NSLog(@"%@", @"Camera access not determined. Ask for permission."); [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { if(granted) { NSLog(@"Granted access to %@", AVMediaTypeVideo); [self popCamera]; } else { NSLog(@"Not granted access to %@", AVMediaTypeVideo); [self camDenied]; } }]; } else if (authStatus == AVAuthorizationStatusRestricted) { // My own Helper class is used here to pop a dialog in one simple line. [Helper popAlertMessageWithTitle:@"Error" alertText:@"You've been restricted from using the camera on this device. Without camera access this feature won't work. Please contact the device owner so they can give you access."]; } else { [self camDenied]; } }
Denial alert:
- (void)camDenied { NSLog(@"%@", @"Denied camera access"); NSString *alertText; NSString *alertButton; BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL); if (canOpenSettings) { alertText = @"It looks like your privacy settings are preventing us from accessing your camera to do barcode scanning. You can fix this by doing the following:\n\n1. Touch the Go button below to open the Settings app.\n\n2. Turn the Camera on.\n\n3. Open this app and try again."; alertButton = @"Go"; } else { alertText = @"It looks like your privacy settings are preventing us from accessing your camera to do barcode scanning. You can fix this by doing the following:\n\n1. Close this app.\n\n2. Open the Settings app.\n\n3. Scroll to the bottom and select this app in the list.\n\n4. Turn the Camera on.\n\n5. Open this app and try again."; alertButton = @"OK"; } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:alertText delegate:self cancelButtonTitle:alertButton otherButtonTitles:nil]; alert.tag = 3491832; [alert show]; }
Delegate call for the UIAlertView:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { if (alertView.tag == 3491832) { BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL); if (canOpenSettings) [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; } }
Once they have denied camera access, the user can authorize camera use for your app in Settings. By design, you can't override this in your own code.
You can detect this case with the following sample code and then explain to the user how to fix it: iOS 7 UIImagePickerController Camera No Image
NSString *mediaType = AVMediaTypeVideo; // Or AVMediaTypeAudio AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType]; // The user has explicitly denied permission for media capture. else if(authStatus == AVAuthorizationStatusDenied){ NSLog(@"Denied"); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With