Ask permission to access Camera Roll

后端 未结 7 1280
生来不讨喜
生来不讨喜 2020-11-28 22:39

I have a settings view where the user can choose switch on or off the feature \'Export to Camera Roll\'

When the user switches it on for the first time (and

7条回答
  •  天命终不由人
    2020-11-28 22:47

    I'm not sure if there is some build in method for this, but an easy way would be to use ALAssetsLibrary to pull some meaningless bit of information from the photo library when you turn the feature on. Then you can simply nullify what ever info you pulled, and you will have prompted the user for access to their photos.

    The following code for example does nothing more than get the number of photos in the camera roll, but will be enough to trigger the permission prompt.

    #import 
    
    ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
    [lib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        NSLog(@"%zd", [group numberOfAssets]);
    } failureBlock:^(NSError *error) {
        if (error.code == ALAssetsLibraryAccessUserDeniedError) {
            NSLog(@"user denied access, code: %zd", error.code);
        } else {
            NSLog(@"Other error code: %zd", error.code);
        }
    }];
    

    EDIT: Just stumbled across this, below is how you can check the authorization status of your applications access to photo albums.

    ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
    
    if (status != ALAuthorizationStatusAuthorized) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Attention" message:@"Please give this app permission to access your photo library in your settings app!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil, nil];
        [alert show];
    }
    

提交回复
热议问题