我有一个应用程序,我在其中用相机拍照并将该图像存储到本机图库中。但是,如果应用程序没有权限,我希望用户知道这一点。那么我该如何检查呢?
顺便说一句:我将图像存储到图库中:
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
您需要检查状态ALAssetLibrary
以确保您已AssetsLibrary/AssetsLibrary.h
包含在文件中
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
// 检查ALAuthorizationStatusAuthorized
or的状态,ALAuthorizationStatusDenied
例如
if (status != ALAuthorizationStatusAuthorized) {
//show alert for asking the user to give permission
}
斯威夫特 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
}
}
如果您正在使用照片框架,因为 ALAsset 库已从 ios 9 中弃用,您可以使用 PHAuthorizationStatus 检查图库访问权限。您还需要导入照片框架。
#import <Photos/Photos.h>
- (BOOL)hasGalleryPermission
{
BOOL hasGalleryPermission = NO;
PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];
if (authorizationStatus == PHAuthorizationStatusAuthorized) {
hasGalleryPermission = YES;
}
return hasGalleryPermission;
}
注意:仅限 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.