当我的应用程序第一次尝试在iOS 8上访问相机时,用户会看到一个相机权限对话框,就像iOS 7中用于麦克风访问的麦克风对话框一样。
在iOS 7中,可以提前调用麦克风权限对话框并查看权限是否被授予(例如,请参见this question )。是否有类似的方法来调用iOS 8中的相机权限对话框?该对话框可以组合为麦克风和相机访问权限吗?
发布于 2014-09-27 06:45:19
下面是我们最后使用的方法:
if ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType: completionHandler:)]) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
// Will get here on both iOS 7 & 8 even though camera permissions weren't required
// until iOS 8. So for iOS 7 permission will always be granted.
if (granted) {
// Permission has been granted. Use dispatch_async for any UI updating
// code because this block may be executed in a thread.
dispatch_async(dispatch_get_main_queue(), ^{
[self doStuff];
});
} else {
// Permission has been denied.
}
}];
} else {
// We are on iOS <= 6. Just do what we need to do.
[self doStuff];
}
发布于 2014-09-23 18:42:53
我遇到了一个类似的问题,如果用户在第一次提示时拒绝了对相机的访问,按下按钮就可以在照相机模式下的黑色屏幕上拍摄快照结果。
但是,我想检测到用户已经拒绝了访问,并提示他们必须打开,但我找不到任何函数来检查当前用户的相机访问,有这样的功能吗?
编辑:下面的检查将让您知道在IOS 8中有关相机访问:
#import <AVFoundation/AVFoundation.h>
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(status == AVAuthorizationStatusAuthorized) { // authorized
}
else if(status == AVAuthorizationStatusDenied){ // denied
}
else if(status == AVAuthorizationStatusRestricted){ // restricted
}
else if(status == AVAuthorizationStatusNotDetermined){ // not determined
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if(granted){ // Access has been granted ..do something
} else { // Access denied ..do something
}
}];
}
这些信息是关于以下问题(How to know that application have camera access or not programmatically in iOS8)的:
发布于 2015-06-27 08:39:34
这是我的Swift解决方案(iOS 8),我需要相机的QR扫描,所以真的必须迅速使用它。
这提供了
为了让它运行调用,检查ViewDidAppear /或ViewDidLoad等中的摄像机。我需要使用viewDidAppear,因此设置了自定义的相机视图约束。
func checkCamera() {
let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
switch authStatus {
case .authorized: break // Do your stuff here i.e. allowScanning()
case .denied: alertToEncourageCameraAccessInitially()
case .notDetermined: alertPromptToAllowCameraAccessViaSetting()
default: alertToEncourageCameraAccessInitially()
}
}
func alertToEncourageCameraAccessInitially() {
let alert = UIAlertController(
title: "IMPORTANT",
message: "Camera access required for QR Scanning",
preferredStyle: UIAlertControllerStyle.alert
)
alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Allow Camera", style: .cancel, handler: { (alert) -> Void in
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
}))
present(alert, animated: true, completion: nil)
}
func alertPromptToAllowCameraAccessViaSetting() {
let alert = UIAlertController(
title: "IMPORTANT",
message: "Please allow camera access for QR Scanning",
preferredStyle: UIAlertControllerStyle.alert
)
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel) { alert in
if AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo).count > 0 {
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { granted in
DispatchQueue.main.async() {
self.checkCamera() } }
}
}
)
present(alert, animated: true, completion: nil)
}
感谢上面给出的使用dispatch_async的提示-使响应显示新设置的相机功能要快得多。
很抱歉尾随的闭包混在一起。想试一试。
https://stackoverflow.com/questions/25803217
复制相似问题