iOS app camera access denied iOS 9.1(black screen)

2019-02-25 05:21发布

I want to access camera in my app. I am trying the following code.

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

This code works on my other app perfectly.But in this app,it is not asking camera permissions or showing it in the settings->privacy->camera.

enter image description here

The app prompts to use the location.But not showing anything for the camera or photos.

The black screen appears and I can't take the picture if I directly use the camera code without the condition check.

3条回答
可以哭但决不认输i
2楼-- · 2019-02-25 05:47

Use following method to check device camera authorizationStatus. If not it will prompt for Access, if rejected if will show alert to navigate to App settings.

- (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;
    }
}
查看更多
唯我独甜
3楼-- · 2019-02-25 05:52

I had the exactly same issue for couple of days,

Try this its solved my problem, make sure that there is a value

(Application name as string) in your info.plist > "Bundle display name".

In my case it was empty and because of that it didn't work.

let me know if it helped you.

查看更多
走好不送
4楼-- · 2019-02-25 05:55

The code works in my app :

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

Don't forget to add UIImagePickerControllerDelegate in your .h

I hope it will work.

查看更多
登录 后发表回答