没有从照片库中挑选照片的权限



这是我的代码,可以让用户选择个人资料照片:

早些时候,我只允许用户用相机拍摄个人资料图像,但我决定更改它,以便用户现在可以从照片库中选择照片。

问题是,我的用户可以访问照片库,而不会收到访问照片库的请求警报"某应用程序希望访问您的照片库";。我有照片库使用和照片库添加使用的Info.plist。此外,当我的应用程序在模拟器中运行时,当我进入"设置",然后进入"隐私"时,我会进入"照片",看看我的应用是否可以访问,但它们没有提到我的应用可以访问照片库。

如果有人能帮我弄清楚我做错了什么,以及如何获得访问"照片库"的请求来工作,我将不胜感激!!非常感谢。


class ProfileImageVC: UIViewController {
@IBOutlet weak var AddAProfilePictureLabel: UILabel!

@IBOutlet weak var ProfilePictureImageView: UIImageView!

@IBOutlet weak var TapToChangeButton: UIButton!

@IBOutlet var ErrorLabel: UILabel!

@IBOutlet var FinishButton: UIButton!

var ImagePicker:UIImagePickerController!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.            
let imageTap = UITapGestureRecognizer(target: self, action: #selector(openImagePicker))
ProfilePictureImageView.isUserInteractionEnabled = true
ProfilePictureImageView.addGestureRecognizer(imageTap)
ProfilePictureImageView.layer.cornerRadius = ProfilePictureImageView.bounds.height / 2
ProfilePictureImageView.clipsToBounds = true
TapToChangeButton.addTarget(self, action: #selector(openImagePicker), for: .touchUpInside)
//when user clicks they can choose a photo from library
//instantiate image picker
ImagePicker = UIImagePickerController()
ImagePicker.allowsEditing = true
ImagePicker.delegate = self
}
// taping to add a photo
@objc func openImagePicker(_ sender:Any) {
// Open Image Picker
#if targetEnvironment(simulator)
// simulator
ImagePicker.sourceType = .photoLibrary
self.present(ImagePicker, animated: true, completion: nil)
#else
// real device
ImagePicker.sourceType = .photoLibrary
self.present(ImagePicker, animated: true, completion: nil)
#endif
}

func checkCameraAccess() {
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .denied:
print("Denied, request permission from settings")
presentCameraSettings()
case .restricted:
print("Restricted, device owner must approve")
case .authorized:
self.present(ImagePicker, animated: true, completion: nil)
case .notDetermined:
AVCaptureDevice.requestAccess(for: .video) { success in
DispatchQueue.main.async {
if success {
self.present(self.ImagePicker, animated: true, completion: nil)
} else {
self.presentCameraSettings()
}
}
}
@unknown default:
break
}
}
func presentCameraSettings() {
let alertController = UIAlertController(title: "Error",
message: "Photo library access is denied",
preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Cancel", style: .default))
alertController.addAction(UIAlertAction(title: "Settings", style: .cancel) { _ in
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url)
}
})
present(alertController, animated: true)
}
}
//extend the proper delagate method
extension ProfileImageVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//cancel
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
//pick an image
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
//get the image the selected
if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
self.ProfilePictureImageView.image = pickedImage
picker.dismiss(animated: true, completion: nil)
startLoading(self.view)
//upload to firbase
PhotoService.savePhoto(image: pickedImage) { error in
stopLoading(self.view)
let resturantslocation =
self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.userslocation) as? ResturantsUsersLocationVC
self.view.window?.rootViewController = resturantslocation
self.view.window?.makeKeyAndVisible()
}
}
}
}

问题是,我的用户可以访问照片库,而不会收到访问照片库的请求警报

这不是一个";问题";。这是正确的行为。

UIImagePickerController(或者,在iOS 14中,PHPickerViewController(不需要照片库的用户授权。这是因为你的应用程序没有访问照片库!运行时正在访问它,用户愿意给你一张照片。你收到的照片,不是图书馆里的一张完整的照片——那将是一张PHAsset——而是一张UIImage。所以你得到的都是无实体的图像。

如果您想转身获取PHAsset并查看库,则需要用户授权。但是您只是要求.editedImage,所以不需要用户授权。

如果.editedImagenil,那是因为它是错误的密钥;如果用户没有编辑图像,那么您想要的是.originalImage。而且你仍然不需要授权。

如果您想要求系统显示请求授权的警报,请继续并要求它这样做。但是用户不会仅仅因为你使用了picker而神奇地得到警报。

用户权限必须考虑的事项

何时需要用户权限

如果你需要访问照片元数据,你需要用户的权限。元数据包括地理位置、创建时间戳和相机信息等

它是如何工作的

看起来应用程序可以访问照片库,但选择器已超时,因此您的应用程序将只接收用户选择的内容。

如果您需要请求用户的许可。添加以下代码片段。

@objc func showAttachmentAction(sender _: UIBarButtonItem) {
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .notDetermined:
PHPhotoLibrary.requestAuthorization { [weak self] status in
guard let self else { return }
if status == .authorized {
DispatchQueue.main.async {
self.present(self.imagePicker, animated: true)
}
}
}
case .restricted, .denied:
let alert = UIAlertController(title: "Permisstion Denied", message: "APPName does not have access to photos. To enable access please go to the device's Settings > Privacy > Photos > AppName", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default)
alert.addAction(okAction)
present(alert, animated: true)
case .authorized:
present(imagePicker, animated: true)
default:
break
}
}

但在请求许可之前,请确保添加info.plist隐私-照片库使用说明。否则,应用程序将崩溃。

最新更新