请求访问相机胶卷的权限

Tit*_*eul 61 objective-c ios

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

当用户第一次打开它时(而不是当他拍摄第一张照片时),我希望该应用程序向他询问是否允许访问相机胶卷.

我在许多应用程序中看到了行为,但无法找到方法.

Mic*_*lum 96

我不确定是否有一些构建方法,但是当你打开这个功能时,一个简单的方法是使用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);
    }
}];
Run Code Online (Sandbox Code Playgroud)

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

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];
}
Run Code Online (Sandbox Code Playgroud)

  • 尼斯.感谢error.code我直接在failureBlock中发出警报. (2认同)

Tom*_*Bąk 90

由于带有Photos框架的 iOS 8 使用:

Swift 3.0:

PHPhotoLibrary.requestAuthorization { status in
    switch status {
    case .authorized:
        <#your code#>
    case .restricted:
        <#your code#>
    case .denied:
        <#your code#>
    default:
        // place for .notDetermined - in this callback status is already determined so should never get here
        break
    }
}
Run Code Online (Sandbox Code Playgroud)

Objective-C:

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
    switch (status) {
        case PHAuthorizationStatusAuthorized:
            <#your code#>
            break;
        case PHAuthorizationStatusRestricted:
            <#your code#>
            break;
        case PHAuthorizationStatusDenied:
            <#your code#>
            break;
        default:
            break;
    }
}];
Run Code Online (Sandbox Code Playgroud)

文档中的重要说明:

此方法始终立即返回.如果用户先前已授予或拒绝照片库访问权限,则在调用时执行处理程序块; 否则,它会显示警报并仅在用户响应警报后执行该块.

  • 对于一些'我没有阅读文档'的原因,代码是在后台线程上运行的.如果你需要ui更改,请将它包装成这样的东西:`dispatch_async(dispatch_get_main_queue(),^ { - ui touch代码在这里 - });` (9认同)
  • 请注意,在iOS 10及更高版本中,您还需要为[NSPhotoLibraryUsageDescription]提供一个字符串(https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//您的Info.plist中的key_ref/doc/uid/TP40009251-SW17)键,或者您对`requestAuthorization`(或任何依赖于Photos授权的API)的调用将崩溃. (4认同)

Jus*_*dow 11

从iOS 10开始,我们还需要在info.plist文件中提供照片库使用说明,我在那里描述.然后只需使用此代码即可在每次需要时显示警报:

- (void)requestAuthorizationWithRedirectionToSettings {
    dispatch_async(dispatch_get_main_queue(), ^{
        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
        if (status == PHAuthorizationStatusAuthorized)
        {
            //We have permission. Do whatever is needed
        }
        else
        {
            //No permission. Trying to normally request it
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                if (status != PHAuthorizationStatusAuthorized)
                {
                    //User don't give us permission. Showing alert with redirection to settings
                    //Getting description string from info.plist file
                    NSString *accessDescription = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSPhotoLibraryUsageDescription"];
                    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:accessDescription message:@"To give permissions tap on 'Change Settings' button" preferredStyle:UIAlertControllerStyleAlert];

                    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
                    [alertController addAction:cancelAction];

                    UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:@"Change Settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                    }];
                    [alertController addAction:settingsAction];

                    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
                }
            }];
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

还有一些常见的情况是警报没有出现.为了避免复制,我希望你看看这个答案.