我有一个设置 View ,用户可以在其中选择打开或关闭“导出到相机胶卷”功能
当用户第一次打开它时(而不是在他拍摄第一张照片时),我希望应用程序询问他访问相机胶卷的权限。
我在许多应用程序中看到了行为,但找不到解决方法。
最佳答案
我不确定是否有一些内置方法可以解决此问题,但一种简单的方法是在您打开该功能时使用 ALAssetsLibrary 从照片库中提取一些无意义的信息。然后,您可以简单地取消您提取的任何信息,并且您将提示用户访问他们的照片。
例如下面的代码只是获取相机胶卷中的照片数量,但足以触发权限提示。
#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);
}
}];
编辑:刚刚偶然发现,以下是如何检查应用程序访问相册的授权状态。
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];
}
关于objective-c - 请求访问相机胶卷的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13572220/