16

I have an app in which I take a picture with the camera and store that image into the native gallery. But if the app doesn't have permission for that, I want the user to know that. So how do I check it?

By the way: I store the image into the gallery with:

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
Pang
  • 9,564
  • 146
  • 81
  • 122
gabrjan
  • 3,080
  • 9
  • 40
  • 69
  • can we change the answer to the correct one, or put version info into the question? – benc Nov 06 '18 at 06:16

4 Answers4

30

You need to check the status of ALAssetLibrary make sure you have AssetsLibrary/AssetsLibrary.h included in your file

  ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];

// check the status for ALAuthorizationStatusAuthorized or ALAuthorizationStatusDenied e.g

    if (status != ALAuthorizationStatusAuthorized) {
        //show alert for asking the user to give permission

    }
nsgulliver
  • 12,655
  • 23
  • 43
  • 64
  • it should work on ios5+ , if you see ALAssetLibrary then you would notice that it has support for ios5+ – nsgulliver Feb 21 '13 at 14:13
  • for further reference you could use [ALAssetLibrary Docs](http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html) – nsgulliver Feb 21 '13 at 14:16
  • well i added AssetsLibrarz.framework but when i do import "ALAssetLibrary.h" i get error that it can not be found – gabrjan Feb 21 '13 at 14:18
  • 3
    Include it like #import – msk Feb 21 '13 at 14:19
5

Swift 3

import photos

PHPhotoLibrary.requestAuthorization { status in
     switch status {
     case .authorized:
          self.processSnapShotPhotos()
     case .restricted:
          print("handle restricted")
     case .denied:
          print("handle denied")     
     default:
       // place for .notDetermined - in this callback status is already determined so should never get here
            break
     }
}
CodeOverRide
  • 4,431
  • 43
  • 36
4

If you are using photos framework since ALAsset libraries are deprecated from ios 9 you can use PHAuthorizationStatus to check gallery access. You need to import photos framework as well.

  #import <Photos/Photos.h>

- (BOOL)hasGalleryPermission
{
    BOOL hasGalleryPermission = NO;
    PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];

    if (authorizationStatus == PHAuthorizationStatusAuthorized) {
        hasGalleryPermission = YES;
    }
    return hasGalleryPermission;
}
Gihan
  • 2,476
  • 2
  • 27
  • 33
3

Note: iOS 6 Only

Is this what you are looking for

[ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusAuthorized;

Other values of authorizationStatus are

ALAuthorizationStatusRestricted,        // This application is not authorized to access photo data.
                                            // The user cannot change this application’s status, possibly due to active restrictions
                                            //  such as parental controls being in place.
    ALAuthorizationStatusDenied,            // User has explicitly denied this application access to photos data.
    ALAuthorizationStatusAuthorized         // User has authorized this application to access photos data.
msk
  • 8,885
  • 6
  • 41
  • 72