简体   繁体   English

如何检测用户已单击“不允许访问摄像头”

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

I am using a UIImagePicker to present the users with camera to take photos which will be used in the app. 我正在使用UIImagePicker向用户展示相机以拍摄将在应用中使用的照片。

My problem is that on the first time a user opens the image picker they are presented with a prompt saying: '"my App" Would like to Access your Camera' with two options, Don't allow and OK. 我的问题是,当用户第一次打开图像选择器时,他们会看到一个提示:'“我的应用程序”想要使用两个选项访问您的相机,不允许和确定。

My requirement is that when the user clicks Don't Allow, the Image picker gets dismissed leaving a black view. 我的要求是,当用户单击“不允许”时,图像选择器将被关闭,留下黑色视图。 Is there a way to detect that the user has chosen Don't allow? 有没有办法检测用户选择了不允许?

Here is my code to present UIImagePicker : 这是我提供UIImagePicker代码:

var PhotoPicker:UIImagePickerController = UIImagePickerController()
PhotoPicker.delegate = self
PhotoPicker.sourceType = .Camera
PhotoPicker.cameraFlashMode = .Off
PhotoPicker.showsCameraControls = false
PhotoPicker.cameraDevice = .Rear
self.presentViewController(PhotoPicker, animated: false, completion: nil)

To detect access to your library: 要检测对库的访问:

You need to use AssetsLibrary for that. 您需要使用AssetsLibrary。 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. 你需要使用AVFoundation。 First, import avfoundation framework: 一,导入avfoundation框架:

import AVFoundation

Then, as previously, request user permission when you go to imagepicker and catch the event. 然后,如前所述,当您转到imagepicker并捕获事件时请求用户权限。

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! 希望能帮助到你!

In iOS 10, use: 在iOS 10中,使用:

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

Check out this for detecting camera permission 看看这个用于检测相机许可

Presenting camera permission dialog in iOS 8 在iOS 8中显示相机权限对话框

Use this when user picks Don't Allow. 用户选择“不允许”时使用此选项。

PhotoPicker.dismissViewControllerAnimated(false, completion: nil)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在用户单击时第二次设置提示第一次不允许权限? - How to set a prompt for second time when user has clicked Don't allow for permissions the first time? 如何检测用户选择不允许MKMapView for iphone - How to detect user selects Don't Allow for MKMapView for iphone xamarin iOS:位置请求如何找出用户点击“允许”或“不允许”的时间 - xamarin iOS: Location Request how to find out when user clicks “Allow” or “Don't allow” is clicked 当用户不允许在iOS中访问时如何关闭imagePicker - How to dismiss imagePicker when user don't allow access in iOS 用户单击“不允许”后显示UILocalNotification请求 - Display UILocalNotification request after user clicked “don't allow” 在首先按下不允许后如何授予对 react-native-camera 的访问权限? - How to grant access to react-native-camera after don't allow is pressed first? 如何检测用户是否在Apple的推送通知确认提醒中点击“不允许” - How to detect if user tap on “Don't Allow” on Apple's push notification confirmation alert 在要求访问照片的权限时,如何对选择“不允许”的用户做出反应? - How do I react to a user choosing “Don't Allow” when asking for permission to access Photos? 如何检测用户点击了相机中的“使用照片”按钮? - How to detect user has tapped “Use Photo” button in the camera? 在更改照片库时检测用户何时点击“不允许” - Detect when user tapped Don't Allow when making changes to photo library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM