jam*_*mix 70 permissions camera ios ios8 ios-permissions
当我的应用程序第一次尝试在iOS 8上访问摄像头时,会向用户显示摄像头权限对话框,非常类似于iOS 7中用于麦克风访问的麦克风.
在iOS 7中,可以事先调用麦克风权限对话框,并查看是否已授予权限(例如,请参阅此问题).是否有类似的方法来调用iOS 8中的相机权限对话框?可以将对话组合用于麦克风和摄像机访问权限吗?
jam*_*mix 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];
}
Run Code Online (Sandbox Code Playgroud)
Smo*_*ugh 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
}
}];
}
Run Code Online (Sandbox Code Playgroud)
此信息可在以下问题中找到(如何在iOS8中以编程方式知道该应用程序是否具有摄像头访问权限):
Dog*_*fee 52
这是我的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)
}
Run Code Online (Sandbox Code Playgroud)
感谢上面的jamix用于使用dispatch_async的提示 - 使响应显示新设置的相机功能更快.
对不起混合的尾随封闭..想试试看.
Cra*_*lot 15
没有任何答案似乎检查麦克风和相机权限.我们的代码会检查授予摄像机权限但麦克风访问被拒绝的情况.
由于我们是Swift的新手,因此粗糙嵌套的闭包和if语句不太可能是最佳的.请分享改进代码的建议!但至少它在测试中起作用.
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (videoGranted: Bool) -> Void in
if (videoGranted) {
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeAudio, completionHandler: { (audioGranted: Bool) -> Void in
if (audioGranted) {
dispatch_async(dispatch_get_main_queue()) {
// Both video & audio granted
}
} else {
// Rejected audio
}
})
} else {
// Rejected video
}
})
Run Code Online (Sandbox Code Playgroud)
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)
}
Run Code Online (Sandbox Code Playgroud)
对于 Swift 3,您可以将其添加到您viewWillAppear的第一个视图控制器的方法中:
首先导入AVFoundation框架
import AVFoundation
Run Code Online (Sandbox Code Playgroud)
然后:
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")
}
}
Run Code Online (Sandbox Code Playgroud)
不要忘记Privacy - Camera Usage Description在您的Info.plist
| 归档时间: |
|
| 查看次数: |
72239 次 |
| 最近记录: |