iOS app相机访问被拒绝iOS 9.1(黑屏)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS app相机访问被拒绝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];
}
}
此代码完全适用于我的其他应用程序。但在此应用程序中,它不是要求相机权限或在settings-> privacy-> camera中显示它。
该应用程序提示使用该位置。但没有显示相机或照片的任何内容。
如果我在没有条件检查的情况下直接使用相机代码,则会出现黑屏并且无法拍照。
答案
几天我有完全相同的问题,
试试这个它解决了我的问题,确保有一个值
(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
我希望它能奏效。
以上是关于iOS app相机访问被拒绝iOS 9.1(黑屏)的主要内容,如果未能解决你的问题,请参考以下文章
iOS开发调用相机时出现黑屏的解决办法(原因:没有获取到相机权限)
在 config.xml 中设置使用说明后,Ionic IOS 相机权限仍然被拒绝