Why does PHPickerViewController say "Unable to Load Photos"

702 Views Asked by At

I'm working on a project where I need the user to be able to select images from their camera roll. I was using a UIImagePickerController but for some reason it's not working anymore which I assume is because they've finally depricated the imagePicker. I'm not using a PHPickerViewController but every time it pops up it says "Unable to Load Photos" with a try again button and even clicking that button doesn't load the photos.

I want the User to be able to select a collectionView cell which makes the PHPickerViewController pop up. Then they select a photo photo and then, once done the collectionView to update with the selected image in the cell that was tapped.

Here's the code where I present the PHPickerViewController:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    var configuration = PHPickerConfiguration(photoLibrary: .shared())
    configuration.selectionLimit = 3
    configuration.filter = .images
    let picker = PHPickerViewController(configuration: configuration)
    picker.delegate = self
    self.present(picker, animated: true, completion: nil)
}

And I use this method inside of viewDidLoad() to check photo permissions:

func photoAuthorization() {
    PHPhotoLibrary.requestAuthorization { status in
        switch status {
        case .notDetermined:
            break
        case .restricted, .denied:
            let alert = UIAlertController(title: "Photo Access restriced or denied",
                                          message: "Please allow access to your photo library",
                                          preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: { (_) in
                alert.dismiss(animated: true)
            }))
            
            DispatchQueue.main.async {
                self.present(alert, animated: true)
            }
        case .authorized:
            break
        case .limited:
            let alert = UIAlertController(title: "Photo Access Limited",
                                          message: "Please allow full access to your photo library",
                                          preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: { (_) in
                alert.dismiss(animated: true)
            }))
            
            DispatchQueue.main.async {
                self.present(alert, animated: true)
            }
        }
    }
}

If someone could let me know if I'm setting the viewController up wrong or if there are additional steps I need to take in order for it to work correctly it would be very much appreciated. Also if there is a better way to achieve this functionality please let me know.

Update: I tried running it on my actual device rather than the simulator and it runs just fine. I don't know why it doesn't work in the simulator, so maybe it has something to do with my laptops settings rather than the code itself? Anyone have any ideas why it would work on my actual phone but not the simulator?

0

There are 0 best solutions below