如何检查我的应用是否可以访问电话库

时间:2013-02-21 13:59:06

标签: ios uiimage gallery

我有一个应用程序,我用相机拍照并将该图像存储到本机库中。但如果应用程序没有权限,我希望用户知道这一点。那我该怎么检查呢?

顺便说一句:我将图像存储在图库中:

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

4 个答案:

答案 0 :(得分:29)

您需要检查ALAssetLibrary的状态 确保您的文件中包含AssetsLibrary/AssetsLibrary.h

  ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];

//检查ALAuthorizationStatusAuthorizedALAuthorizationStatusDenied例如

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

    }

答案 1 :(得分:4)

如果您使用的是照片框架,因为从ios 9中弃用了ALAsset库,您可以使用PHAuthorizationStatus来检查图库访问。您还需要导入照片框架。

  #import <Photos/Photos.h>

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

    if (authorizationStatus == PHAuthorizationStatusAuthorized) {
        hasGalleryPermission = YES;
    }
    return hasGalleryPermission;
}

答案 2 :(得分:4)

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
     }
}

答案 3 :(得分:3)

注意:仅限iOS 6

这就是你要找的东西

[ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusAuthorized;

authorizationStatus的其他值是

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.