objective-c - 请求访问相机胶卷的权限

标签 objective-c ios

我有一个设置 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/

相关文章:

ios - 只要函数持续就显示 UIAlertController

ios - 在表格 View 单元格内隐藏按钮

ios - 有效地检查核心数据中是否存在项目

c++ - NSURLConnection 和 NSURLRequest 的纯 C++ 等价物

objective-c - 从 NSTextView Objective-C 获取选择(突出显示的文本)字符串

objective-c - 如何欺骗 OS X 应用程序认为鼠标是手指?

ios - TableView 部分分隔线

objective-c - UITableView 自定义 UIView 重复

ios - 未调用 subview Controller 旋转方法

ios - EXC_BAD_ACCESS 仅当从 UITableView 中一直向下滚动返回时