I use the following code to check and request authorization for the Camera. Problem is the following. The following scenario leads to a wrong authorization status:
- User declines authorization for the first time
- Terminates the app
- Restarts the app
- Leaves the application, grants authorization for the camera in settings app
- Returns to the app
[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]
will return AVAuthorizationStatusDeclined
(authorized as said).
After terminating and restarting results in AVAuthorizationStatusAuthorized
as it should. In this case the user leaves to settings and denies camera access the result will remain AVAuthorizationStatusAuthorized
until next restart.
Any ideas what I miss here?
- (void) popCamera { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; //picker.allowsEditing = YES; #if !(TARGET_IPHONE_SIMULATOR) picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.cameraDevice = UIImagePickerControllerCameraDeviceFront; #endif self.view.translatesAutoresizingMaskIntoConstraints = YES; [self presentViewController:picker animated:YES completion:NULL];}- (void)camDenied{ NSLog(@"%@", @"Denied camera access"); NSString *alertText; NSString *alertButton; BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL); if (canOpenSettings) { alertText = LSS(@"DeniedCamera1"); SDCAlertView *alert = [[SDCAlertView alloc] initWithTitle:LSS(@"DeniedCameraTitle") message:alertText delegate:self cancelButtonTitle:LSS(@"Cancel") otherButtonTitles:LSS(@"Goto"), nil]; alert.tag = 3491832; [alert show]; } else { alertText = LSS(@"DeniedCamera2"); SDCAlertView *alert = [[SDCAlertView alloc] initWithTitle:LSS(@"DeniedCameraTitle") message:alertText delegate:self cancelButtonTitle:LSS(@"Cancel") otherButtonTitles:nil]; alert.tag = 3491832; [alert show]; }}- (IBAction) onTakePhoto:(id)sender { 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) { [self popCamera]; } else { [self camDenied]; } }]; } else if (authStatus == AVAuthorizationStatusRestricted) { SDCAlertView *alert = [[SDCAlertView alloc] initWithTitle:LSS(@"RestrictCameraTitle") message:LSS(@"RestrictCamera") delegate:self cancelButtonTitle:LSS(@"OK") otherButtonTitles:nil]; } else { [self camDenied]; } }
Credits for the original code: Is there a way to ask user for Camera access after they have already denied it on iOS 8?