How to detect user has clicked Don't Allow access to camera

随声附和 提交于 2019-12-01 18:08:09

To detect access to your library:

You need to use AssetsLibrary for that. First, import assets library framework:

import AssetsLibrary

Then, request authorization status, and if it is not determined, use blocks to catch those events, like this:

if ALAssetsLibrary.authorizationStatus() == ALAuthorizationStatus.NotDetermined {

    let library = ALAssetsLibrary()
    library.enumerateGroupsWithTypes(.All, usingBlock: { (group, stop) -> Void in

        // User clicked ok
    }, failureBlock: { (error) -> Void in

        // User clicked don't allow
        imagePickerController.dismissViewControllerAnimated(true, completion: nil)
    })
}

To detect access to camera:

You need to use AVFoundation for that. First, import avfoundation framework:

import AVFoundation

Then, as previously, request user permission when you go to imagepicker and catch the event.

if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) == AVAuthorizationStatus.NotDetermined {

    AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (videoGranted: Bool) -> Void in

        // User clicked ok
        if (videoGranted) {

        // User clicked don't allow
        } else {
            imagePickerController.dismissViewControllerAnimated(true, completion: nil)
        }
    })
}

Hope it helps!

Anish Kumar

In iOS 10, use:

import Photos

let authStatus = PHPhotoLibrary.authorizationStatus()
if authStatus == .notDetermined || authStatus == .denied {
    PHPhotoLibrary.requestAuthorization({ (status) in
        if status == PHAuthorizationStatus.authorized {

        } else {
            imagePickerController.dismissViewControllerAnimated(true, completion: nil)
        }
    })
}
Rasputin

Check out this for detecting camera permission

Presenting camera permission dialog in iOS 8

Use this when user picks Don't Allow.

PhotoPicker.dismissViewControllerAnimated(false, completion: nil)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!