有没有办法要求用户访问相机访问后,他们已经否认了iOS 8?

我正在使用这个代码,但不幸的是它不工作。

在用户拒绝摄像头访问后,我想要求他们在下次尝试加载摄像头时再次使用摄像头(本例中是使用摄像头视图的条形码扫描器)。 我总是得到AVAuthorizationStatusDenied ,然后granted总是自动返回NO即使我再次要求代码。

我的许多用户正在给我发电子邮件说:“当我尝试条形码扫描时,我的屏幕是黑色的”,这是因为他们由于某种原因拒绝了相机访问。 我希望能够再次提醒他们,因为很可能拒绝是一个错误。

有没有可能的方法来做到这一点?

  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."); } 

经过一番研究,看起来你不能做我想做的事情。 这里是我编码的另一种方式,popup一个对话框,并在iOS 8+上自动打开“设置”应用程序。

 - (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]; } } 

拒绝提醒:

 - (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. Touch Privacy.\n\n3. Turn the Camera on.\n\n4. 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. Touch Privacy.\n\n5. Turn the Camera on.\n\n6. 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]; } 

委托调用UIAlertView:

 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { if (alertView.tag == 3491832) { BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL); if (canOpenSettings) [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; } } 

一旦他们拒绝摄像头访问,用户可以在“设置”中为您的应用程序授权使用摄像头。 按照devise,你不能在你自己的代码中覆盖它。

您可以使用以下示例代码检测此案例,然后向用户解释如何解决该问题: 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"); } 

对于Swift 3.0

这将导致用户更改权限的设置。

 func checkCameraAuthorise() -> Bool { let status = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) if status == .restricted || status == .denied { let dialog = ZAlertView(title: "", message: "Please allow access to the camera in the device's Settings -> Privacy -> Camera", isOkButtonLeft: false, okButtonText: "OK", cancelButtonText: "Cancel", okButtonHandler: { _ -> Void in UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)}, cancelButtonHandler: { alertView in alertView.dismissAlertView() }) dialog.show() return false } return true }