当我的应用程序第一次尝试在iOS 8上访问摄像头时,会向用户显示一个摄像头权限对话框,就像iOS 7中用于麦克风访问的麦克风一样。
在iOS 7中,可以预先调用麦克风权限对话框,并查看是否已授予权限(例如,请参阅this question)。是否有类似的方法来调用iOS 8中的相机权限对话框?可以组合对话框以获得麦克风和摄像机访问权限吗?
答案 0 :(得分:88)
以下是我们最终使用的方法:
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];
}
答案 1 :(得分:61)
我遇到了类似的问题,如果用户在第一次提示时拒绝了相机访问权限,则按下按钮拍摄快照会导致相机模式下出现黑屏。
但是,我想检测用户拒绝访问并提示他们必须打开但我找不到任何功能来检查当前用户的摄像头访问权限,是否有这样的功能?
编辑:以下检查将在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)中找到:
答案 2 :(得分:50)
这是我的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)
}
感谢上面的jamix提示使用dispatch_async - 使响应显示新设置的相机功能更快。
很抱歉混合使用尾随封闭..想试一试。
答案 3 :(得分:15)
答案似乎都没有检查麦克风和相机权限。我们的代码会检查授予摄像机权限但麦克风访问被拒绝的情况。
由于我们是Swift的新手,因此粗糙嵌套的闭包和%AppData%\Roaming\serverpat/uploadpdf/
语句不太可能是最佳的。请分享改进代码的建议!但至少它在测试中起作用。
if
答案 4 :(得分:8)
Swift 3.0解决方案
导入AVFoundation
注意:在Info.plist上添加隐私 - 相机使用说明密钥
// MARK:相机处理
func callCamera(){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = UIImagePickerControllerSourceType.camera
self.present(myPickerController, animated: true, completion: nil)
NSLog("Camera");
}
func checkCamera() {
let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
switch authStatus {
case .authorized: callCamera() // Do your stuff here i.e. callCameraMethod()
case .denied: alertToEncourageCameraAccessInitially()
case .notDetermined: alertPromptToAllowCameraAccessViaSetting()
default: alertToEncourageCameraAccessInitially()
}
}
func alertToEncourageCameraAccessInitially() {
let alert = UIAlertController(
title: "IMPORTANT",
message: "Camera access required for capturing photos!",
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: "Camera access required for capturing photos!",
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)
}
答案 5 :(得分:4)
对我来说,iOS7和iOS8上的这项工作:
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
switch (status) {
case ALAuthorizationStatusAuthorized:
break;
case ALAuthorizationStatusRestricted:
case ALAuthorizationStatusDenied:
break;
case ALAuthorizationStatusNotDetermined:
break;
}
答案 6 :(得分:4)
对于Swift 3,您可以在第一个视图控制器的viewWillAppear
方法中添加它:
首先导入AVFoundation
框架
import AVFoundation
然后:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let authorizationStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
switch authorizationStatus {
case .notDetermined:
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { granted in
if granted {
print("access granted")
}
else {
print("access denied")
}
}
case .authorized:
print("Access authorized")
case .denied, .restricted:
print("restricted")
}
}
不要忘记在Privacy - Camera Usage Description
Info.plist
密钥
答案 7 :(得分:3)
我对应用代表进行访问检查。
nm <- read.csv("http://www.sgi.com/tech/mlc/db/churn.names",
skip=4, colClasses=c("character", "NULL"), header=FALSE, sep=":")[[1]]
nm
dat <- read.csv("http://www.sgi.com/tech/mlc/db/churn.data", header=FALSE, col.names=c(nm, "Churn"))
dat
View(dat)
View(dat)
library(survival)
s <- with(dat, Surv(account.length, as.numeric(Churn)))
model <- coxph(s ~ total.day.charge + number.customer.service.calls, data=dat[, -4])
summary(model)
plot(survfit(model))
*
答案 8 :(得分:2)
我遇到的问题是,由于最近的一些构建配置更改,我的Info.plist中没有设置Bundle Display Name
和{{1}}。一种不太可能的案例......但是我花了几个小时来解决这个问题。希望它对其他人有帮助。