如何在IOS上获得照片/相机的授权状态?

时间:2015-03-14 14:33:06

标签: ios uiimagepickercontroller

我正在使用UIImagePickerController。我需要告诉我的用户他们需要去设置 - >隐私来解锁相机/照片访问权限,如果他们不同意提供访问权限,我想首先显示他们UIImagePickerController

默认情况下,UIImagePickerController会显示:

enter image description here

但是在呈现UIImagePickerController之前我想要这条消息怎么样?我怎样才能得到是否有访问权限的消息?

我想声称这不是一个绝对重复的问题。此问题中的AVCaptureDevice detect permission of camera on ios仅修复了对相机的访问权限,但访问了相册'仍然无法被发现。

2 个答案:

答案 0 :(得分:5)

问题detect permission of camera on ios提到了课程AVCaptureDevice,此课程可以修复“相机”的授权。

我找到了'照片'授权的解决方案。在IOS8中有一个名为Photos的新框架和一个类PHPhotoLibrary

此方法可以提供警报:

PHPhotoLibrary.requestAuthorization({(status:PHAuthorizationStatus)in
    switch status{
    case .Denied:
        break
    case .Authorized:
        break
    default:
        break
    }
})

enter image description here

就像第一次出现UIImagePickerController时一样。

class func authorizationStatus() -> PHAuthorizationStatus可以返回“相册”的当前授权状态

答案 1 :(得分:3)

AVCaptureDevice为此提供API。

        if (![[AVCaptureDevice  class] respondsToSelector:@selector(authorizationStatusForMediaType:)])
        {
            //Do something…
            break;
        }

        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        switch (authStatus)
        {
            case AVAuthorizationStatusAuthorized:
            case AVAuthorizationStatusRestricted:
            {
                //Do something…
                break;
            }
            case AVAuthorizationStatusDenied:
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"App name" message:@"Appname does not have access to your camera. To enable access, go to iPhone Settings > AppName and turn on Camera." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];

                //Do something…
                break;
            }
            case AVAuthorizationStatusNotDetermined:
            {
                // not determined?!
                [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
                 {
                     if(granted)
                     {
                         dispatch_async(dispatch_get_main_queue(), ^
                         {
                             //Do something…
                         });
                     }
                     else
                     {
                         dispatch_async(dispatch_get_main_queue(), ^
                         {
                             //Do something…
                         });
                     }
                 }];
                break;
            }
            default:
                break;
        }