请求访问相机胶卷的权限

时间:2012-11-26 20:02:34

标签: objective-c ios

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

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

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

7 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:90)

由于带有Photos framework的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
    }
}

<强>目标C

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

来自documentation的重要提示:

  

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

答案 2 :(得分:11)

自iOS 10起,我们还需要在info.plist文件中提供照片库使用说明,我将其描述为there。然后只需使用此代码即可在每次需要时显示警告:

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

还有一些常见的情况是警报没有出现。为避免复制,我希望您查看此answer

答案 3 :(得分:4)

用户第一次尝试在ios 6上写入相机胶卷时,会自动要求他/她进行许可。您不必添加额外的代码(在此之前,authorisationstatus是ALAuthorizationStatusNotDetermined)。

如果用户第一次否认你不能再问(据我所知)。用户必须在settings-&gt; privacy-&gt;中手动更改该应用特定设置。照片部分。

还有另外一个选项,那就是由于家长控制等其他限制,用户无法授予权限,在这种情况下状态为ALAuthorizationStatusRestricted

答案 4 :(得分:4)

夫特:

import AssetsLibrary

var status:ALAuthorizationStatus = ALAssetsLibrary.authorizationStatus()

if status != ALAuthorizationStatus.Authorized{
    println("User has not given authorization for the camera roll")
}

答案 5 :(得分: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;
    }

答案 6 :(得分:0)

iOS 9.2.1,Xcode 7.2.1,已启用ARC

  

'ALAuthorizationStatus'已弃用:在iOS 9.0中首先弃用 -   改为在Photos框架中使用PHAuthorizationStatus

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

Determine if the access to photo library is set or not - PHPhotoLibrary (iOS 8)

主要提示:

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

最简单的是......

if ([PHPhotoLibrary class])
{
   //Use the Photos framework
}
else
{
   //Use the Asset Library framework
}
  • 您需要决定要将哪个媒体收藏用作源,这取决于您应用的设备。将运行它正在使用的操作系统版本。

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