9

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?

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

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)
}
scottc00
  • 207
  • 2
  • 6
  • Does the picker appear? – Tamás Sengel Feb 16 '18 at 11:46
  • delete the app from phone and try again. – Ankit Kumar Gupta Feb 16 '18 at 11:56
  • The picker appears and allows me to choose an image. I have deleted the app, and reset privacy settings - still doesn't ask for permission – scottc00 Feb 16 '18 at 11:59
  • Also add this : Privacy - Photo Library Additions Usage Description – Sharad Chauhan Feb 16 '18 at 12:06
  • I solved this problem. my issues are xCode location. xCode location is Desktop that time permission popup not coming, move to the application folder after restart my computer and worked well. – AshvinGudaliya Feb 16 '18 at 12:08
  • I tried both of those. I have other projects stored on the desktop and they ask for permission. – scottc00 Feb 16 '18 at 12:27
  • Wish I could answer this - Apple's way of keeping us on our toes? Anyways, if you are using iOS 10, there's a different key: https://stackoverflow.com/questions/39519773/nsphotolibraryusagedescription-key-must-be-present-in-info-plist-to-use-camera-r If this is the issue, let me know - I'll try to mark this as duplicate (it will helps others and isn't a rep hit for you). – dfd Feb 16 '18 at 13:53
  • 2
    I'm using iOS11. It's so frustrating! It works how I want it but doesn't ask for permission so I know that would be rejected if I submit to the App store. Even if I take my photo library entries out of the info.plist. uninstall the app, reinstall and run, access to the photo library is granted with no crash or problems. I thought the app would crash if no entries are in the info.plist - Or so I read on the Apple dev website! – scottc00 Feb 16 '18 at 15:11

3 Answers3

17

The answer is simple: it's normal behavior since iOS 11, because the UIImagePickerController runs in a separate process and therefore for just read-only access you don't need any special permissions.

falsecrypt
  • 1,336
  • 11
  • 10
9

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.")
    }
}
scottc00
  • 207
  • 2
  • 6
  • Thank u for solution. only thing missing is PHPhotoLibrary.requestAuthorization is done in background thread. so the line for showing picker should be brought to main thread with like "DispatchQueue.main.async { self.present(self.imagePicker, animated: true, completion: nil) }" – Njuacha Hubert May 05 '21 at 08:25
4

According to apple documentation

Note

When using the UIImagePickerController to bring up the user's photo library, your app doesn't need to request permission explicitly.

Photos automatically prompts the user to request authorization when needed.

Community
  • 1
  • 1