我在一个应用程序中实现了以下代码,允许用户向应用程序添加图像。但是,用户永远不会被要求访问照片的权限。我错过了什么?
在我的info.plist中,我设置了:隐私-照片库使用说明=请提供对照片库的访问权限
捆绑包标识符为:$(PRODUCT_BUNDLE_IDENTIFIER)
import UIKit
class NewPostXIB: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var image: UIImageView!
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
self.view.endEditing(true)
}
@IBAction func closeButton(_ sender: Any) {
dismiss(animated: false, completion: nil)
}
@IBAction func addImageButton(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
image.contentMode = .scaleAspectFill
image.image = pickedImage
}
dismiss(animated: false, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
3条答案
按热度按时间pzfprimi1#
答案很简单:这是iOS 11以来的正常行为,因为
UIImagePickerController
在单独的进程中运行,因此对于只读访问,您不需要任何特殊权限。0yg35tkg2#
根据apple documentation
注意
使用UIImagePickerController调出用户的照片库时,您的应用无需显式请求权限。
Photos会在需要时自动提示用户请求授权。
bn31dyow3#
添加以下函数解决了问题: