Request permission to access Camera Roll

I have a settings view in which the user can choose to enable or disable the Export to Camera Roll function

When the user first turns it on (and not when he takes the first image), I would like the application to ask him for permission to access the camera’s video.

I have seen behavior in many applications, but cannot find a way to do this.

+57
ios objective-c
Nov 26
source share
7 answers

I'm not sure if there is any build method for this, but an easy way is to use ALAssetsLibrary to pull a meaningless bit of information from the photo library when you turn on this feature. Then you can simply cancel all the information you pulled out and you ask the user to access their photos.

The following code, for example, no more than allows you to get the number of photos in the camera frame, but will be enough to trigger a permission request.

#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 upon this, the following shows how you can check the authorization status of your apps to access 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]; } 
+94
Nov 26 '12 at 20:24
source share

Since iOS 8 with Frame Frame uses:

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 the documentation :

This method always returns immediately. If the user has previously granted or denied access to the library library, he executes the handler block when called; otherwise, it displays a warning and executes the block only after the user has answered the warning.

+86
Oct 30 '14 at 14:50
source share

With iOS 10, we also need to provide a description of using the photo library in the info.plist file that I described there . And then just use this code so that a warning appears every time:

 - (void)requestAuthorizationWithRedirectionToSettings { dispatch_async(dispatch_get_main_queue(), ^{ PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus]; if (status == PHAuthorizationStatusAuthorized) { //We have permission. Do whatever is needed } else { //No permission. Trying to normally request it [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { if (status != PHAuthorizationStatusAuthorized) { //User don't give us permission. Showing alert with redirection to settings //Getting description string from info.plist file NSString *accessDescription = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSPhotoLibraryUsageDescription"]; UIAlertController * alertController = [UIAlertController alertControllerWithTitle:accessDescription message:@"To give permissions tap on 'Change Settings' button" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]; [alertController addAction:cancelAction]; UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:@"Change Settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; }]; [alertController addAction:settingsAction]; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil]; } }]; } }); } 

There are also some common cases where a warning does not appear. To avoid copying, I would like you to look at this answer .

+10
Jul 15 '16 at 14:02
source share

The first time a user tries to record a camera frame on ios 6, he is automatically asked for permission. You do not need to add additional code (before this authorization state is ALAuthorizationStatusNotDetermined).

If the user denies the first time, you cannot ask again (as far as I know). The user must manually change this application parameter in the settings section β†’ privacy-> photos.

There is one more option, and this means that the user cannot give permission due to other restrictions, such as parental control, in this case the status is ALAuthorizationStatusRestricted

+4
Jan 25 '13 at 16:34
source share

Swift:

 import AssetsLibrary var status:ALAuthorizationStatus = ALAssetsLibrary.authorizationStatus() if status != ALAuthorizationStatus.Authorized{ println("User has not given authorization for the camera roll") } 
+4
Dec 25 '14 at 10:17
source share
 #import <AssetsLibrary/AssetsLibrary.h> 

//////

 ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; switch (status) { case ALAuthorizationStatusRestricted: { //Tell user access to the photos are restricted } break; case ALAuthorizationStatusDenied: { // Tell user access has previously been denied } break; case ALAuthorizationStatusNotDetermined: case ALAuthorizationStatusAuthorized: // Try to show image picker myPicker = [[UIImagePickerController alloc] init]; myPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; myPicker.delegate = self; [self presentViewController: myPicker animated:YES completion:NULL]; break; default: break; } 
+2
Apr 08 '15 at 15:03
source share

iOS 9.2.1, Xcode 7.2.1, ARC enabled

"ALAuthorizationStatus" deprecated: deprecated first in iOS 9.0 - Use PHAuthorizationStatus in the framework instead

Please review this post for an updated solution:

Determine if access to the photo library is set or not - PHPhotoLibrary (iOS 8)

Key notes:

  • Most likely, you are developing iOS7.0 + today, because of this you will need to process both ALAuthorizationStatus and PHAuthorizationStatus .

The easiest way to do this ...

 if ([PHPhotoLibrary class]) { //Use the Photos framework } else { //Use the Asset Library framework } 
  • You will need to decide which media collection you want to use as a source, this is dictated by the device that your application is using. will work and what version of the OS it uses.

  • You might want to direct the user to the settings if the user is denied authorization.

0
Feb 15 '16 at 5:50
source share



All Articles