如何检查用户是否授予使用相机的权限? [英] How to check if the user gave permission to use the camera?
本文介绍了如何检查用户是否授予使用相机的权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试写这个:
if usergavepermissiontousercamera
opencamera
else
showmycustompermissionview
找不到执行此简单任务的当前方法.
注意:即使需要其他方法,iOS7也应该可以使用
Couldn't find a current way to do this simple task.
Note: Should also work iOS7 even if it requires a different method
推荐答案
您可以使用以下代码执行相同的操作:
You can use the following code for doing the same:
if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) == AVAuthorizationStatus.Authorized {
// Already Authorized
} else {
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
if granted == true {
// User granted
} else {
// User rejected
}
})
}
注意:
- 确保在构建阶段的链接二进制"部分中添加
AVFoundation
框架 - 您应该在课堂上写
import AVFoundation
以便导入AVFoundation
- Make sure that you add the
AVFoundation
Framework in the Link Binary section of build phases - You should write
import AVFoundation
on your class for importingAVFoundation
SWIFT 3
if AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) == AVAuthorizationStatus.authorized {
// Already Authorized
} else {
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
if granted == true {
// User granted
} else {
// User Rejected
}
})
}
雨燕4
if AVCaptureDevice.authorizationStatus(for: .video) == .authorized {
//already authorized
} else {
AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) in
if granted {
//access allowed
} else {
//access denied
}
})
}
这篇关于如何检查用户是否授予使用相机的权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文