Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ask permission to access Camera Roll

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 not when he takes the first picture), I would like the app to ask him the permission to access the camera roll.

I've seen behavior in many app but can't find the way to do it.

like image 983
Titouan de Bailleul Avatar asked Nov 26 '12 20:11

Titouan de Bailleul


People also ask

What does access to camera roll mean?

When you give access to an app to photos (either through the prompt that comes when the app tries to access photos or through Settings > Privacy > Photos), the app gets access to write new photos/images to your Camera Roll and to read all your photos on the device in an unencrypted form.


2 Answers

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 <AssetsLibrary/AssetsLibrary.h>  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]; } 
like image 118
Mick MacCallum Avatar answered Oct 15 '22 09:10

Mick MacCallum


Since iOS 8 with Photos framework use:

Swift 3.0:

PHPhotoLibrary.requestAuthorization { status in     switch status {     case .authorized:         <#your code#>     case .restricted:         <#your code#>     case .denied:         <#your code#>     default:         // place for .notDetermined - in this callback status is already determined so should never get here         break     } } 

Objective-C:

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {     switch (status) {         case PHAuthorizationStatusAuthorized:             <#your code#>             break;         case PHAuthorizationStatusRestricted:             <#your code#>             break;         case PHAuthorizationStatusDenied:             <#your code#>             break;         default:             break;     } }]; 

Important note from documentation:

This method always returns immediately. If the user has previously granted or denied photo library access permission, it executes the handler block when called; otherwise, it displays an alert and executes the block only after the user has responded to the alert.

like image 27
Tomasz Bąk Avatar answered Oct 15 '22 09:10

Tomasz Bąk