Search code examples
iosobjective-calauthorizationstatus

How to handle authorization status not determined for first time


Utilities.m

+(AVAuthorizationStatus)getAuthorizationStatus {
    NSString *mediaType = AVMediaTypeAudio;
    __block AVAuthorizationStatus AuthStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    if(AuthStatus == AVAuthorizationStatusNotDetermined) {
        [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
            AuthStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
        }];
    }
    NSLog(@"%d",AuthStatus);
    return AuthStatus;
}

+(ALAuthorizationStatus)getALAssetAuthorizationStatus {
    ALAssetsLibrary *aLib = [[ALAssetsLibrary alloc]init];

    ALAuthorizationStatus alAuthStatus;

    [aLib enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:nil failureBlock:nil];

    alAuthStatus = [ALAssetsLibrary authorizationStatus];

    NSLog(@"Status=>%d", [ALAssetsLibrary authorizationStatus]);

    return alAuthStatus;
}

In my utilities class I wrote these two methods and there purpose is just to tell what is the authorization status.

These method will work fine if user have already allowed or denied the permission, but for first time these methods ask user for permission and the authorization status is not determined. In case of first time if user allow or deny I am not getting the updated authorization status. How to handle this?


Solution

  • In your completion handler of requestAccessForMediaType, rather than re-calling authorizationStatusForMediaType, you just need to refer to the 'granted' boolean that's passed to the block (which you're not currently using).

    I know this is an old question, but I stumbled across while looking for something related and figured I'd answer it for others who might find it.