【问题标题】:UIImagePickerController not asking for permissionUIImagePickerController 不请求许可
【发布时间】:2018-07-27 07:41:54
【问题描述】:

我在应用中实现了以下代码,以允许用户向应用添加图像。然而,用户永远不会被要求获得访问照片的权限。我在这里错过了什么?

在我的 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)
}

【问题讨论】:

  • 选择器出现了吗?
  • 从手机中删除该应用,然后重试。
  • 选择器出现并允许我选择图像。我已经删除了该应用,并重置了隐私设置 - 仍然没有请求许可
  • 另外添加:隐私 - 照片库添加使用说明
  • 我用的是iOS11。这太令人沮丧了!它按我想要的方式工作,但不要求许可,所以我知道如果我提交到 App Store 会被拒绝。即使我将我的照片库条目从 info.plist 中取出。卸载应用程序,重新安装并运行,授予对照片库的访问权限,没有崩溃或问题。我认为如果 info.plist 中没有条目,应用程序会崩溃 - 或者我在 Apple dev 网站上阅读!

标签: ios swift uiimagepickercontroller info.plist


【解决方案1】:

添加以下函数解决了这个问题:

 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.")
    }
}

【讨论】:

  • 感谢您的解决方案。唯一缺少的是 PHPhotoLibrary.requestAuthorization 在后台线程中完成。所以显示选择器的行应该被带到主线程,比如“DispatchQueue.main.async { self.present(self.imagePicker, animated: true, completion: nil) }”
【解决方案2】:

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

【讨论】:

  • 这就是我想要的,它让我免于阅读文档,非常感谢
【解决方案3】:

根据apple documentation

注意

使用 UIImagePickerController 调出用户照片时 库,您的应用不需要显式请求权限。

照片会在需要时自动提示用户请求授权。

【讨论】:

  • 这仍然是真的吗?
猜你喜欢
  • 1970-01-01
  • 2015-07-11
  • 1970-01-01
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-15
相关资源
最近更新 更多