iOS Photos and Camera Detect Permission Tutorial

Welcome again to my new tutorial. This is the best way to detect and ask permission from your user. This tutorial will give your app a better UX (user experience) whenever the user disables the permission to your app. So let's get started.

You will simply read and copy paste the codes below, I will explain also if it is needed.

This is the code for checking the permission to Camera Access.

#pragma mark - check permission camera
- (void)checkPermissionForCamera
{
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        if(granted){
            NSLog(@"user has granted access to camera, do nothing...");
        } else {
            [self showAlertNoPermission:YES];
        }
    }];
}

And this is the code for checking the permission to Photo Gallery Access.
#pragma mark - check permission photo gallery
- (void)checkPermissionForPhotos
{
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    
    switch (status)
    {
        case PHAuthorizationStatusAuthorized:
            NSLog(@"user has authorized the photos, do nothing...");
            break;
        case PHAuthorizationStatusNotDetermined:
        {
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus authorizationStatus)
             {
                 if (authorizationStatus == PHAuthorizationStatusAuthorized)
                 {
                     NSLog(@"user has authorized the photos, do nothing...");
                 }
                 else
                 {
                     [self showAlertNoPermission:NO];
                 }
             }];
            break;
        }
        default:
            [self showAlertNoPermission:NO];
            break;

So I will explain the codes now. In our checkPermissionForCamera method, we will check here the permission for camera access, if the user has given the right permission, then do nothing. You can also deleted the NSLog to make your app better. Else (not), then the method will call another method, which is named: showAlertNoPermission. This method showAlertNoPermission will be posted below. It accepts one parameter, a boolean. If YES is pass, then it will show a certain message, if NO, then it will give a different message to the user.

Next, the checkPermissionForPhotos method. The explanation is simple and the same with the explanation above. You will see that there are two ways to check if the user has authorized the app to access the user's photo gallery. If the authorizationStatus is not Authorized, then it will call (again) the showAlertNoPermission method with a NO parameter.

Here is the showAlertNoPermission method.

- (void)showAlertNoPermission:(BOOL)forCamera
{
    UIAlertController *noPermissionAlert = [UIAlertController
                                             alertControllerWithTitle:@""
                                             message:@""
                                             preferredStyle:UIAlertControllerStyleAlert];
    
    NSMutableAttributedString *customFontAttrib;
    
    if (forCamera) {
        customFontAttrib = [[NSMutableAttributedString alloc] initWithString:@"We need your permission\n This app needs to access your camera.\nGo to Settings > THE_NAME_OF_YOURAPP > Camera > Switch ON."];
    }
    
    else {
        customFontAttrib = [[NSMutableAttributedString alloc] initWithString:@"We need your permission\n This app needs to access your photo library.\nGo to Settings > THE_NAME_OF_YOURAPP > Photos > Switch ON."];
    }
    
    [customFontAttrib addAttribute:NSFontAttributeName
                             value:[UIFont systemFontOfSize:14.0]
                             range:NSMakeRange(22, customFontAttrib.length-22)];
    [customFontAttrib addAttribute:NSFontAttributeName
                             value:[UIFont systemFontOfSize:18.0 weight:10.0]
                             range:NSMakeRange(0, 23)];
    [customFontAttrib addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor]
                             range:NSMakeRange(0,customFontAttrib.length)];
    
    [noPermissionAlert setValue:customFontAttrib forKey:@"attributedTitle"];
    
    UIAlertAction *ok = [UIAlertAction
                         actionWithTitle:@"Ok, I got it!"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             //Do some thing here
                             [noPermissionAlert dismissViewControllerAnimated:YES completion:nil];
                         }];
    
    [noPermissionAlert addAction:ok];
    
    // UIAlertController BG
    UIView * firstView = noPermissionAlert.view.subviews.firstObject;
    UIView * nextView = firstView.subviews.firstObject;
    nextView.backgroundColor = [UIColor avHighlightColor];
    
    noPermissionAlert.view.tintColor = [UIColor whiteColor];
    
    [self presentViewController:noPermissionAlert animated:YES completion:nil];
}

There you go guys. This codes are from my project and I wrote it. If this tutorial helped you, then share it and subscribe.

Post a Comment