我如何检查我的应用程序是否可以访问手机库

我有一个应用程序,我用相机拍照,并将图像存储到本地画廊。 但是,如果应用程序没有这个权限,我想让用户知道。 那么我该如何检查呢?

顺便说一句:我将图像存储到画廊中:

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); 

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

  ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; 

//检查ALAuthorizationStatusAuthorizedALAuthorizationStatusDenied的状态,例如

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

注意:仅限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. 

如果您正在使用照片框架,因为ALAsset库已从ios 9弃用,您可以使用PHAuthorizationStatus检查图库访问权限。 您还需要导入照片框架。

  #import <Photos/Photos.h> - (BOOL)hasGalleryPermission { BOOL hasGalleryPermission = NO; PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus]; if (authorizationStatus == PHAuthorizationStatusAuthorized) { hasGalleryPermission = YES; } return hasGalleryPermission; } 

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