请求访问相机胶卷的权限 [英] Ask permission to access Camera Roll

查看:156
本文介绍了请求访问相机胶卷的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置视图,用户可以选择打开或关闭功能'导出到相机胶卷'

I have a settings view where the user can choose switch on or off the feature 'Export to Camera Roll'

当用户第一次打开它(而不是当他拍摄第一张照片时),我希望应用程序让他获得访问相机胶卷的许可。

When the user switches it on for the first time (and not when he takes the first picture), I would like the app to ask him the permission to access the camera roll.

我在许多应用程序中都看到了行为,但找不到办法。

I've seen behavior in many app but can't find the way to do it.

推荐答案

我不确定是否有一些构建方法,但是当你打开这个功能时,一个简单的方法是使用ALAssetsLibrary从照片库中提取一些无意义的信息。 。然后,您可以简单地取消您所提取的信息,并且您将提示用户访问他们的照片。

I'm not sure if there is some build in method for this, but an easy way would be to use ALAssetsLibrary to pull some meaningless bit of information from the photo library when you turn the feature on. Then you can simply nullify what ever info you pulled, and you will have prompted the user for access to their photos.

以下代码仅举例说明了相机胶卷中的照片数量,但足以触发权限提示。

The following code for example does nothing more than get the number of photos in the camera roll, but will be enough to trigger the permission prompt.

#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);
    }
}];

编辑:偶然发现了这一点,下面是你如何检查您的应用程序访问相册的授权状态。

Just stumbled across this, below is how you can check the authorization status of your applications access to photo albums.

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

这篇关于请求访问相机胶卷的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆