0

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?

JonGrimes20
  • 115
  • 9
  • Did you setup permissions? If not, do so. If yes, try to use `PHPickerConfiguration(photoLibrary: .shared())` <-- loads system library – timbre timbre Apr 25 '22 at 21:17
  • @ohglstr I'm pretty sure I have but I may have done it incorrectly can you post an answer of how to set it up correctly? This is my first time using photokit so I'm a bit lost on what exactly im supposed to do – JonGrimes20 Apr 25 '22 at 21:23
  • you didn't share much details on what you are trying to do. I suggest reviewing this guide: https://developer.apple.com/documentation/photokit/delivering_an_enhanced_privacy_experience_in_your_photos_app Before that though: did you try to specify a photo library like I suggested above? (`PHPickerConfiguration(photoLibrary: .shared())`). I don't see anything wrong with the code you posted otherwise. – timbre timbre Apr 25 '22 at 21:39
  • @ohglstr yes I did try specifying the photo library to use and the issue was still there. I'll edit the question to provide more details. – JonGrimes20 Apr 25 '22 at 21:42
  • Could this be related? https://stackoverflow.com/questions/69306179/phpickerviewcontroller-tapping-on-search-gets-error-unable-to-load-photos?rq=1 – matt Apr 25 '22 at 22:16
  • @matt I've check out that post and tried to fix it how he has in his UIKit example but it still isn't working for some reason – JonGrimes20 Apr 25 '22 at 22:31

0 Answers0