매우 간단한 비디오 앱을 개발 중입니다. 나는 공식 컨트롤을 사용합니다 : UIImagePickerController.
여기에 문제가 있습니다. UIImagePickerController를 처음으로 제시하면 iOS에서 권한을 요청합니다. 사용자는 예 또는 아니오를 클릭 할 수 있습니다. 사용자가 아니오를 클릭하면 제어가 해제되지 않습니다. 대신 사용자가 시작 버튼을 계속 클릭하면 화면이 항상 검은 색인 동안 타이머가 켜지고 타이머를 중지하거나 되돌아 갈 수 없습니다. 사용자가 할 수있는 유일한 일은 앱을 종료하는 것입니다. 다음에 UIImagePickerController가 표시 될 때 여전히 검은 색 화면이며 시작을 클릭하면 사용자가 되돌아 갈 수 없습니다.
버그인지 궁금합니다. UIImagePickerController를 표시할지 여부를 결정할 수 있도록 카메라의 권한을 감지 할 수있는 방법이 있습니까?
답변
를 확인하고 AVAuthorizationStatus
케이스를 올바르게 처리하십시오.
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if(authStatus == AVAuthorizationStatusAuthorized) {
// do your logic
} else if(authStatus == AVAuthorizationStatusDenied){
// denied
} else if(authStatus == AVAuthorizationStatusRestricted){
// restricted, normally won't happen
} else if(authStatus == AVAuthorizationStatusNotDetermined){
// not determined?!
[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
if(granted){
NSLog(@"Granted access to %@", mediaType);
} else {
NSLog(@"Not granted access to %@", mediaType);
}
}];
} else {
// impossible, unknown authorization status
}
답변
스위프트 4 이상
다음을 확인하십시오 :
import AVFoundation
아래 코드는 가능한 모든 권한 상태를 확인합니다.
let cameraMediaType = AVMediaType.video
let cameraAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: cameraMediaType)
switch cameraAuthorizationStatus {
case .denied: break
case .authorized: break
case .restricted: break
case .notDetermined:
// Prompting user for the permission to use the camera.
AVCaptureDevice.requestAccess(for: cameraMediaType) { granted in
if granted {
print("Granted access to \(cameraMediaType)")
} else {
print("Denied access to \(cameraMediaType)")
}
}
}
iOS 10부터 NSCameraUsageDescription
카메라 액세스를 요청하려면 Info.plist에서 키 를 지정해야합니다
. 그렇지 않으면 앱이 런타임에 중단됩니다. 사용법 설명이 필요한 API를 참조하십시오 .
흥미로운 부수적으로, 설정에서 카메라 권한을 변경하는 동안 iOS가 앱을 실행하면 앱이 종료된다는 것을 알고 있습니까?
Apple 개발자 포럼에서 :
사용자 가 설정에서 앱의 카메라 액세스를 전환하면 시스템이 실제로 앱 을 종료합니다. 설정 → 개인 정보 섹션의 보호 된 데이터 클래스에도 동일하게 적용됩니다.
답변
스위프트 솔루션
extension AVCaptureDevice {
enum AuthorizationStatus {
case justDenied
case alreadyDenied
case restricted
case justAuthorized
case alreadyAuthorized
case unknown
}
class func authorizeVideo(completion: ((AuthorizationStatus) -> Void)?) {
AVCaptureDevice.authorize(mediaType: AVMediaType.video, completion: completion)
}
class func authorizeAudio(completion: ((AuthorizationStatus) -> Void)?) {
AVCaptureDevice.authorize(mediaType: AVMediaType.audio, completion: completion)
}
private class func authorize(mediaType: AVMediaType, completion: ((AuthorizationStatus) -> Void)?) {
let status = AVCaptureDevice.authorizationStatus(for: mediaType)
switch status {
case .authorized:
completion?(.alreadyAuthorized)
case .denied:
completion?(.alreadyDenied)
case .restricted:
completion?(.restricted)
case .notDetermined:
AVCaptureDevice.requestAccess(for: mediaType, completionHandler: { (granted) in
DispatchQueue.main.async {
if granted {
completion?(.justAuthorized)
} else {
completion?(.justDenied)
}
}
})
@unknown default:
completion?(.unknown)
}
}
}
그런 다음 사용하려면
AVCaptureDevice.authorizeVideo(completion: { (status) in
//Your work here
})
답변
@Raptor의 답변 외에도 다음을 언급해야합니다. iOS 10부터 다음과 같은 오류가 발생할 수 있습니다.This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
이 문제를 해결하려면 다음과 같이 메인 스레드의 결과를 처리해야합니다 (Swift 3).
private func showCameraPermissionPopup() {
let cameraMediaType = AVMediaTypeVideo
let cameraAuthorizationStatus = AVCaptureDevice.authorizationStatus(forMediaType: cameraMediaType)
switch cameraAuthorizationStatus {
case .denied:
NSLog("cameraAuthorizationStatus=denied")
break
case .authorized:
NSLog("cameraAuthorizationStatus=authorized")
break
case .restricted:
NSLog("cameraAuthorizationStatus=restricted")
break
case .notDetermined:
NSLog("cameraAuthorizationStatus=notDetermined")
// Prompting user for the permission to use the camera.
AVCaptureDevice.requestAccess(forMediaType: cameraMediaType) { granted in
DispatchQueue.main.sync {
if granted {
// do something
} else {
// do something else
}
}
}
}
}
답변
Info.plist에서 NSCameraUsageDescription 키를 먼저 지정하십시오. 승인 된 경우 AVAuthorizationStatus를 확인한 다음 UIImagePickerController를 표시하십시오. 작동합니다.
답변
스위프트 : AVFoundation 사용
- AVFoundation을 대상-> 빌드 단계-> 바이너리와 라이브러리 연결에 추가하십시오.
- ViewController에서 AVFoundation을 가져옵니다.
- Info.plist에서 다음을 추가하십시오.
- View Controller에서 :
@IBAction func cameraButtonClicked (보낸 사람 : AnyObject) {
let authorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
print(authorizationStatus.rawValue)
if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) == AVAuthorizationStatus.Authorized{
self.openCameraAfterAccessGrantedByUser()
}
else
{
print("No Access")
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted :Bool) -> Void in
if granted == true
{
// User granted
self.openCameraAfterAccessGrantedByUser()
}
else
{
// User Rejected
alertToEncourageCameraAccessWhenApplicationStarts()
}
});
}
}
//Open camera
func openCameraAfterAccessGrantedByUser()
{
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
self.cameraAndGalleryPicker!.sourceType = UIImagePickerControllerSourceType.Camera
cameraAndGalleryPicker?.delegate = self
cameraAndGalleryPicker?.allowsEditing = false
cameraAndGalleryPicker!.cameraCaptureMode = .Photo
cameraAndGalleryPicker!.modalPresentationStyle = .FullScreen
presentViewController(self.cameraAndGalleryPicker!, animated: true, completion: nil)
}
else
{
}
}
//Show Camera Unavailable Alert
func alertToEncourageCameraAccessWhenApplicationStarts()
{
//Camera not available - Alert
let cameraUnavailableAlertController = UIAlertController (title: "Camera Unavailable", message: "Please check to see if it is disconnected or in use by another application", preferredStyle: .Alert)
let settingsAction = UIAlertAction(title: "Settings", style: .Destructive) { (_) -> Void in
let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString)
if let url = settingsUrl {
dispatch_async(dispatch_get_main_queue()) {
UIApplication.sharedApplication().openURL(url)
}
}
}
let cancelAction = UIAlertAction(title: "Okay", style: .Default, handler: nil)
cameraUnavailableAlertController .addAction(settingsAction)
cameraUnavailableAlertController .addAction(cancelAction)
self.window?.rootViewController!.presentViewController(cameraUnavailableAlertController , animated: true, completion: nil)
}
답변
