UIImagePickerController没有请求权限

问题描述 投票:3回答:2

我在应用程序中实现了以下代码,以允许用户将图像添加到应用程序。但是,永远不会要求用户访问照片的权限。我在这里错过了什么?

在我的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)
}
ios swift uiimagepickercontroller info.plist
2个回答
4
投票

添加以下功能解决了以下问题:

 func checkPermission() {
    let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
    switch photoAuthorizationStatus {
    case .authorized:
        present(imagePicker, animated: true, completion: nil)
        print("Access is granted by user")
    case .notDetermined:
        PHPhotoLibrary.requestAuthorization({
            (newStatus) in
            print("status is \(newStatus)")
            if newStatus ==  PHAuthorizationStatus.authorized {
                /* do stuff here */
                self.present(self.imagePicker, animated: true, completion: nil)
                print("success")
            }
        })
        print("It is not determined until now")
    case .restricted:
        // same same
        print("User do not have access to photo album.")
    case .denied:
        // same same
        print("User has denied the permission.")
    }
}

0
投票

答案很简单:这是iOS 11以来的正常行为,因为UIImagePickerController在一个单独的进程中运行,因此对于只读访问,您不需要任何特殊权限。

© www.soinside.com 2019 - 2024. All rights reserved.