iOS app相机访问被拒绝iOS 9.1(黑屏)

时间:2015-12-14 06:23:03

标签: ios objective-c ipad camera ios9.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];
                }
            }

此代码适用于我的其他应用。但在此应用中,它不是要求相机权限或在设置 - >隐私 - >相机中显示它。

enter image description here

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

如果我在没有条件检查的情况下直接使用相机代码,则会出现黑屏,我无法拍照。

3 个答案:

答案 0 :(得分:2)

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

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

info.plist中的

(应用程序名称为字符串)> “捆绑显示名称”。

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

让我知道它是否对你有帮助。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

代码适用于我的应用:

UIImagePickerControllerDelegate

不要忘记在.h

中添加imageUrlList.add(image_url); imageUrlList.add(image_url1); imageUrlList.add(image_url2); imageUrlList.add(image_url3); imageUrlList.add(image_url4); imageUrlList.add(image_url5); imageUrlList.add(image_url6);

我希望它能奏效。