我有一个设置视图,用户可以 Select 打开或关闭功能"Export to Camera Roll"
当用户第一次打开它时(而不是在他拍摄第一张照片时),我希望该应用程序向他请求访问相机卷的权限.
我在很多应用程序中都见过这种行为,但都找不到方法.
我有一个设置视图,用户可以 Select 打开或关闭功能"Export to Camera Roll"
当用户第一次打开它时(而不是在他拍摄第一张照片时),我希望该应用程序向他请求访问相机卷的权限.
我在很多应用程序中都见过这种行为,但都找不到方法.
我不确定是否有一些内置的方法来实现这一点,但一个简单的方法是在打开该功能时使用AlassetLibrary从照片库中提取一些无意义的信息.然后,你可以简单地取消你所获取的任何信息,你就会提示用户访问他们的照片.
例如,下面的代码只获取相机卷中的照片数量,但足以触发权限提示.
#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:刚刚偶然发现这一点,下面是如何判断应用程序访问相册的授权状态.
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];
}