61

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

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

我已经在许多应用程序中看到了行为,但找不到解决方法。

4

7 回答 7

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

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

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];
}
于 2012-11-26T20:24:26.200 回答
91

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

斯威夫特 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
    }
}

目标-C

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

文档中的重要说明:

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

于 2014-10-30T14:50:39.223 回答
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];
                }
            }];
        }
    });
}

此外,在一些常见情况下,警报不会出现。为避免复制,我希望您看看这个答案

于 2016-07-15T14:02:05.067 回答
4

用户第一次尝试在 ios 6 上写入相机胶卷时,系统会自动要求他/她获得许可。您不必添加额外的代码(在授权状态为 ALAuthorizationStatusNotDetermined 之前)。

如果用户第一次拒绝你不能再问(据我所知)。用户必须在设置->隐私->照片部分手动更改该应用程序的特定设置。

还有另一种选择,即由于其他限制(例如家长控制),用户无法授予权限,在这种情况下,状态为 ALAuthorizationStatusRestricted

于 2013-01-25T16:34:30.527 回答
4

迅速:

import AssetsLibrary

var status:ALAuthorizationStatus = ALAssetsLibrary.authorizationStatus()

if status != ALAuthorizationStatus.Authorized{
    println("User has not given authorization for the camera roll")
}
于 2014-12-25T10:17:11.943 回答
2
#import <AssetsLibrary/AssetsLibrary.h>

//////

ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
    switch (status) {
        case ALAuthorizationStatusRestricted:
        {
            //Tell user access to the photos are restricted
        }

            break;
        case ALAuthorizationStatusDenied:
        {
            // Tell user access has previously been denied
        }

            break;

        case ALAuthorizationStatusNotDetermined:
        case ALAuthorizationStatusAuthorized:

            // Try to show image picker
            myPicker = [[UIImagePickerController alloc] init];
            myPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            myPicker.delegate = self;
            [self presentViewController: myPicker animated:YES completion:NULL];
            break;


        default:
            break;
    }
于 2015-04-08T15:03:05.683 回答
0

iOS 9.2.1、Xcode 7.2.1、ARC 已启用

'ALAuthorizationStatus' 已弃用:首先在 iOS 9.0 中弃用 - 在照片框架中改用 PHAuthorizationStatus

请参阅此帖子以获取更新的解决方案:

确定是否设置了对照片库的访问 - PHPhotoLibrary (iOS 8)

关键说明:

  • 截至今天,您很可能正在为 iOS7.0+ 进行设计,因此您需要同时处理ALAuthorizationStatusPHAuthorizationStatus.

最简单的就是做...

if ([PHPhotoLibrary class])
{
   //Use the Photos framework
}
else
{
   //Use the Asset Library framework
}
  • 您将需要决定要用作来源的媒体集合,这取决于您的应用程序的设备。将在其上运行以及它使用的是哪个版本的操作系统。

  • 如果用户拒绝授权,您可能希望将用户定向到设置。

于 2016-02-15T05:50:32.647 回答