Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Request Access to Camera

I have an app with a QR Code Scanner that works fine, but on iOS 8 the default access to the camera is "Denied". Therefore I have to go into settings and manually give the app access to use the camera. How can I make the prompt that says something like "Would you like to give this app access to use the Camera"?

Here is a sample of my code checking for camera permissions and then requesting permission if the user has not given them. However, the link to give permissions never shows up and eventually just shows the UIAlertView. The status is indeed DENIED when I test, so is there a reason that it is not asking for permissions? Thanks!

Also I have #import AVFoundation/AVFoundation.h so that is not the issue.

-(void) checkCameraAuthorization {

AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

if(status == AVAuthorizationStatusAuthorized) { // authorized
    NSLog(@"camera authorized");
}
else if(status == AVAuthorizationStatusDenied){ // denied
    if ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType: completionHandler:)]) {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            // Will get here on both iOS 7 & 8 even though camera permissions weren't required
            // until iOS 8. So for iOS 7 permission will always be granted.

            NSLog(@"DENIED");

            if (granted) {
                // Permission has been granted. Use dispatch_async for any UI updating
                // code because this block may be executed in a thread.
                dispatch_async(dispatch_get_main_queue(), ^{
                    //[self doStuff];
                });
            } else {
                // Permission has been denied.
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not Authorized" message:@"Please go to Settings and enable the camera for this app to use this feature." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
                [alert show];
            }
        }];
    }
}
else if(status == AVAuthorizationStatusRestricted){ // restricted
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not Authorized" message:@"Please go to Settings and enable the camera for this app to use this feature." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];
}
else if(status == AVAuthorizationStatusNotDetermined){ // not determined

    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        if(granted){ // Access has been granted ..do something
            NSLog(@"camera authorized");
        } else { // Access denied ..do something
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not Authorized" message:@"Please go to Settings and enable the camera for this app to use this feature." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
        }
    }];
}
}
like image 298
MSU_Bulldog Avatar asked Aug 31 '15 12:08

MSU_Bulldog


People also ask

How do I ask for Camera permission iOS?

Step 1: Go to Settings > Privacy. Step 2: Tap on Camera to see which apps have access to it. You can allow or block apps using Camera from here.

Why wont my Iphone allow apps to access my Camera?

Go to Settings-->>Privacy-->>Camera and turn the settings to ON for each app that you would like to have access to it. Go to Settings-->>Privacy-->>Photos and make sure that those apps have the Read & Write permissions.


1 Answers

Sounds like the app has already been denied access to the camera. In this case, you can't prompt again. You can only prompt the user for access once per install. After that you'll need to direct the user to settings.

Send the user to your settings where the Camera can be enabled with this (on iOS8):

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

If you are testing, try deleting the app from the phone and then install and run it again. This puts you back into the AVAuthorizationStatusNotDetermined state.

like image 107
Dan Loughney Avatar answered Oct 04 '22 19:10

Dan Loughney