【问题标题】:iOS app camera access denied iOS 9.1(black screen)iOS 应用程序相机访问被拒绝 iOS 9.1(黑屏)
【发布时间】:2016-03-19 13:45:10
【问题描述】:

我想在我的应用中访问相机。我正在尝试以下代码。

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

此代码在我的其他应用程序上完美运行。但在此应用程序中,它不会询问相机权限或在设置->隐私->相机中显示它。

应用提示使用该位置。但没有显示任何相机或照片。

没有条件检查直接使用相机代码出现黑屏无法拍照。

【问题讨论】:

  • 查看此链接以获取您的解决方案:stackoverflow.com/a/16756715/3378413
  • 不行。我试过了。
  • 你用的是哪个ios版本?
  • 转到设置并向下滚动并点击您的应用。并验证是否显示了摄像头?
  • 不显示。我已在此处添加图像。它仅显示位置访问。查看图像。它来自我的 iPad。

标签: ios objective-c ipad camera ios9.1


【解决方案1】:

代码在我的应用程序中有效:

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

我希望它会起作用。

【讨论】:

    【解决方案2】:

    这几天我遇到了完全相同的问题,

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

    (应用程序名称作为字符串)在您的 info.plist >“捆绑显示名称”中。

    在我的情况下,它是空的,因此它不起作用。

    如果对你有帮助,请告诉我。

    【讨论】:

    • 您只遇到了 ios9 设备的问题?
    • 我在使用 ios8 + ios9 时遇到了这个问题。当我在“捆绑显示名称”中输入一个值(一些字符串)时,一切都已修复。你试过了吗?
    • 非常感谢......它正在工作......自从我开始工作以来已经一个月了。这是一个很大的帮助。
    • 很高兴听到这个消息,祝你好运!
    【解决方案3】:

    使用以下方法检查设备摄像头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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多