[英]privacy issues with camera and photo library in swift
My app initially had no problems accessing both camera and photo library on various devices. 我的应用程序最初在各种设备上访问相机和照片库均没有问题。
Now I find that on some devices I can't get access to the camera or photo library, and the app does not appear at all in the privacy settings after I have tried to access camera and photos. 现在,我发现在某些设备上我无法访问相机或照片库,并且在尝试访问相机和照片后,该应用程序根本没有出现在隐私设置中。
No matter what I do I can't get IOS to recognize my app. 无论我做什么,我都无法让IOS识别我的应用程序。
What can I do to to access the camera and photo library, and have my app appear in the privacy settings? 我该怎么做才能访问相机和照片库,并使我的应用程序出现在隐私设置中? code: 码:
@IBAction func openCameraButton(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) == true {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera//UIImagePickerControllerSourceType.Camera;
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}else{
noCamera()
}
}
@IBAction func openPhotoLibraryButton(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) == true {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
imagePicker.allowsEditing = true
self.presentViewController(imagePicker, animated: true, completion: nil)
}else{
noAccess()
}
}
I use this code for checking accessibly and requesting it if needed: 我使用此代码来进行可访问性检查并在需要时请求它:
import AVFoundation
import Photos
func getCameraAccessibilityAndRequestIfNeeded(completion: (isAccesible: Bool)->Void) {
let authorizationState = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
switch authorizationState {
case .NotDetermined:
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (didAllow) in
completion(isAccesible: didAllow)
})
case .Restricted:
completion(isAccesible: false)
case .Denied:
completion(isAccesible: true)
case .Authorized:
completion(isAccesible: true)
}
}
func getPhotoRollAccessibilityAndRequestIfNeeded(completion: (isAccesible: Bool)->Void) {
PHPhotoLibrary.requestAuthorization { (status) in
switch status {
case .Authorized:
completion(isAccesible: true)
case .Restricted, .Denied , .NotDetermined:
completion(isAccesible: false)
}
}
}
Usage: 用法:
self.getCameraAccessibilityAndRequestIfNeeded { (isAccesible) in
if isAccesible {
// Access confirmed
} else {
// No access
}
}
self.getPhotoRollAccessibilityAndRequestIfNeeded { (isAccesible) in
if isAccesible {
// Access confirm
} else {
// No access
}
}
In the end the answer was very simple: 最后,答案很简单:
I was doing let imagePicker = UIImagePickerController() within the button actions moved it to below class name and camera permissions were back. 我正在做的是在按钮操作中让imagePicker = UIImagePickerController()将其移动到类名以下,并且恢复了摄像头的权限。
I have now tested my code on all IOS devices - on actual devices iPhone 4s and iPod 5 touch and the rest on simulators - the only devices that continue to not allow access to Photo Library and also do not show the app in Privacy are the iPhone 6's iPad Air 2 and retina - so it seems maybe its too do with the display. 我现在已经在所有IOS设备上测试了我的代码-在实际的设备iPhone 4s和iPod 5 touch上以及其余的在模拟器上进行了测试-唯一继续不允许访问照片库并且也不在“隐私”中显示该应用程序的设备是iPhone 6的iPad Air 2和视网膜-似乎显示器也是如此。
Has anyone else had this problem? 有没有其他人有这个问题?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.