Teddy
Teddy

Reputation: 1095

iOS camera authorization - wrong status

I use the following code to check and request authorization for the Camera. Problem is the following. The following scenario leads to a wrong authorization status:

  1. User declines authorization for the first time
  2. Terminates the app
  3. Restarts the app
  4. Leaves the application, grants authorization for the camera in settings app
  5. Returns to the app

[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo] will return AVAuthorizationStatusDeclined (authorized as said).

After terminating and restarting results in AVAuthorizationStatusAuthorized as it should. In this case the user leaves to settings and denies camera access the result will remain AVAuthorizationStatusAuthorized until next restart.

Any ideas what I miss here?

- (void) popCamera {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    //picker.allowsEditing = YES;
    #if !(TARGET_IPHONE_SIMULATOR)
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    #endif
    self.view.translatesAutoresizingMaskIntoConstraints = YES;
    [self presentViewController:picker animated:YES completion:NULL];
}

- (void)camDenied
{
    NSLog(@"%@", @"Denied camera access");

    NSString *alertText;
    NSString *alertButton;

    BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL);
    if (canOpenSettings)
    {
        alertText = LSS(@"DeniedCamera1");

        SDCAlertView *alert = [[SDCAlertView alloc]
                              initWithTitle:LSS(@"DeniedCameraTitle")
                              message:alertText
                              delegate:self
                              cancelButtonTitle:LSS(@"Cancel")
                              otherButtonTitles:LSS(@"Goto"), nil];
        alert.tag = 3491832;
        [alert show];
    }
    else
    {
        alertText = LSS(@"DeniedCamera2");

        SDCAlertView *alert = [[SDCAlertView alloc]
                              initWithTitle:LSS(@"DeniedCameraTitle")
                              message:alertText
                              delegate:self
                              cancelButtonTitle:LSS(@"Cancel")
                              otherButtonTitles:nil];
        alert.tag = 3491832;
        [alert show];
    }

}

- (IBAction) onTakePhoto:(id)sender {
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if(authStatus == AVAuthorizationStatusAuthorized)
    {
        [self popCamera];
    }
    else if(authStatus == AVAuthorizationStatusNotDetermined)
    {
        NSLog(@"%@", @"Camera access not determined. Ask for permission.");

        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
         {
             if(granted)
             {
                 [self popCamera];
             }
             else
             {
                 [self camDenied];
             }
         }];
    }
    else if (authStatus == AVAuthorizationStatusRestricted)
    {
        SDCAlertView *alert = [[SDCAlertView alloc]
                              initWithTitle:LSS(@"RestrictCameraTitle")
                              message:LSS(@"RestrictCamera")
                              delegate:self
                              cancelButtonTitle:LSS(@"OK")
                              otherButtonTitles:nil];
    }
    else
    {
        [self camDenied];
    }

    }

Credits for the original code: Is there a way to ask user for Camera access after they have already denied it on iOS 8?

Upvotes: 2

Views: 860

Answers (1)

DarkDust
DarkDust

Reputation: 92335

This seems to be the intended behaviour. If Apple would want you to react to authorization changes at runtime, there would be a notification that tells you that it changed.

But right now, there is no such notification (as far as I can see). You just call +authorizationStatusForMediaType: and it either returns a definitive status (like denied or authorized), or it returns AVAuthorizationStatusNotDetermined to tell you that you need to request authorization via requestAccessForMediaType:completionHandler:.

Unfortunately, this isn't an authoritative answer; I'm just drawing conclusions and guessing here. You might want to ask on Apple's developer forum and hope to get an answer from an Apple engineer.

Upvotes: 0

Related Questions