iOS应用程序相机访问被拒绝iOS 9.1(黑屏)** **

我想在我的应用程序访问相机。我正在尝试下面的代码。

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypeCamera; if(isIOS8SystemVersion) { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [self presentViewController:picker animated:YES completion:NULL]; }]; } else { [self presentViewController:picker animated:YES completion:NULL]; } } 

这个代码完美的工作在我的其他应用程序。但在这个应用程序,它不要求相机的权限或显示在设置 – >隐私 – >相机。

在这里输入图像说明

该应用程序提示使用该位置,但不显示相机或照片的任何内容。

出现黑屏,如果直接使用相机代码而没有进行条件检查,则无法拍照。

我有几天完全相同的问题,

试试这个解决了我的问题,确保有一个价值

(应用程序名称作为string)在你的info.plist>“Bundle display name”中。

在我的情况下,它是空的,因为它没有工作。

让我知道,如果它帮助你。

使用以下方法检查设备摄像机authorizationStatus 。 如果没有,它会提示进入,如果拒绝,如果将显示警报导航到应用程序设置。

 - (void)checkCameraPermission { // *** check for hardware availability *** BOOL isCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; if(!isCamera) { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:APPName message:@"Camera not detected" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; return; } // *** Store camera authorization status *** AVAuthorizationStatus _cameraAuthorizationStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; switch (_cameraAuthorizationStatus) { case AVAuthorizationStatusAuthorized: { _cameraAuthorizationStatus = AVAuthorizationStatusAuthorized; // *** Camera is accessible, perform any action with camera *** } break; case AVAuthorizationStatusNotDetermined: { NSLog(@"%@", @"Camera access not determined. Ask for permission."); [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { if(granted) { NSLog(@"Granted access to %@", AVMediaTypeVideo); // *** Camera access granted by user, perform any action with camera *** } else { NSLog(@"Not granted access to %@", AVMediaTypeVideo); // *** Camera access rejected by user, perform respective action *** } }]; } break; case AVAuthorizationStatusRestricted: case AVAuthorizationStatusDenied: { // Prompt for not authorized message & provide option to navigate to settings of app. dispatch_async( dispatch_get_main_queue(), ^{ NSString *message = NSLocalizedString( @"My App doesn't have permission to use the camera, please change privacy settings", @"Alert message when the user has denied access to the camera" ); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:APPName message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString( @"OK", @"Alert OK button" ) style:UIAlertActionStyleCancel handler:nil]; [alertController addAction:cancelAction]; // Provide quick access to Settings. UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:NSLocalizedString( @"Settings", @"Alert button to open Settings" ) style:UIAlertActionStyleDefault handler:^( UIAlertAction *action ) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; }]; [alertController addAction:settingsAction]; [self presentViewController:alertController animated:YES completion:nil]; }); } break; default: break; } } 

该代码在我的应用程序中工作:

 UIImagePickerController *picker; if([self checkForCameraAcess]) { picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:picker animated:YES completion:nil]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Camera",nil) message:NSLocalizedString(@"Access to camera seems to be turned off. Please enable it from settings",nil) delegate:self cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:NSLocalizedString(@"Settings",nil), nil]; alert.tag = 101; [alert show]; } -(BOOL)checkForCameraAcess { BOOL isAccess = YES; if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) { AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; //Here we check condition for AVAuthorizationStatusNotDetermined, because when user install iLeads app first time in device (Nerver before iLeads app install in Device), then setup an event and tap on the scan button, at that time authStatus is AVAuthorizationStatusNotDetermined so its show alert for camera acess first. Then after our custom alert shows if we tap on 'Dont allow' button of the camera acess. if(authStatus == AVAuthorizationStatusAuthorized || authStatus == AVAuthorizationStatusNotDetermined) { isAccess = YES; } else { isAccess = NO; } } return isAccess; } 

不要忘记在你的.h中添加UIImagePickerControllerDelegate

我希望这会起作用。