UIImagePickerController 不请求许可 [英] UIImagePickerController not asking for permission

查看:29
本文介绍了UIImagePickerController 不请求许可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用中实现了以下代码,以允许用户向应用添加图像.但是,从不要求用户获得访问照片的权限.我在这里错过了什么?

I have implemented the following code in an app to allow the user to add an image to the app. However the user is never asked for permission to access photo's. What am I missing here?

在我的 info.plist 中,我设置了:隐私 - 照片库使用说明 = 请提供对您照片库的访问权限

in my info.plist I have set: Privacy - Photo Library Usage Description = Please provide access to your photo library

捆绑包标识符是:$(PRODUCT_BUNDLE_IDENTIFIER)

The bundle identifier is: $(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)
}

推荐答案

添加如下函数解决了问题:

Adding the following function solved the problem:

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

这篇关于UIImagePickerController 不请求许可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆