8

In order to fetch photo's creationDate, so use requestAuthorizationForAccessLevel before show PHPickerViewController.

    PHAccessLevel level = PHAccessLevelReadWrite;
    [PHPhotoLibrary requestAuthorizationForAccessLevel:level handler:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusLimited || status == PHAuthorizationStatusAuthorized) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    PHPickerConfiguration *configuration = [[PHPickerConfiguration alloc] initWithPhotoLibrary:[PHPhotoLibrary sharedPhotoLibrary]];
                    configuration.filter = [PHPickerFilter imagesFilter];
                    configuration.selectionLimit = 1;
                    PHPickerViewController *picker = [[PHPickerViewController alloc] initWithConfiguration:configuration];
                    picker.delegate = self;
                    [self showViewController:picker sender:nil];
                });
            }
    }];

although status is .limited, but iOS 14 still display all images.

How can i get only limited photos with PHPickerViewController?

Mr.Laity
  • 133
  • 1
  • 8

1 Answers1

8

So a couple of things got changed in iOS 14, let's see step by step

1. How to read PHPhotoLibrary access permission status

Old

let status = PHPhotoLibrary.authorizationStatus()

New

let status = PHPhotoLibrary.authorizationStatus(for: .readWrite)

2. How to request PHPhotoLibrary access permission

Old

PHPhotoLibrary.requestAuthorization { status in
 //your code               
 }

New

PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
      switch status {
          case .limited:
               print("limited access granted")
                
          default:
               print("denied, .restricted ,.authorized")
                
      }
  }

It is your responsibility to show gallery like below sample code in case of user granted you limited permission

if status == .limited {
     PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)
}

When you presentLimitedLibraryPicker the selected images from the previous session would be already marked check, along with a message on top of screen- "Select more photos or deselect to remove access"

enter image description here

In-case the user granted you limited access still you present the normal gallery using UIImagePickerController or a third party library like BSImagePicker, a gallery with all pictures would be shown even you can select and import into your app but in Xcode 12 console it will show warnings as below

Failed to decode image
[ImageManager] Failed to get sandbox extension for url: file///filepath/5003.JPG, error: Error Domain=com.apple.photos.error Code=41008 "Invalid asset uuid for client" UserInfo={NSLocalizedDescription=Invalid asset uuid for client}
Sheshnath
  • 3,293
  • 1
  • 32
  • 60
  • 2
    Great that's really helpful, I wasn't aware of the new .readWrite authorization. Without it, the .limited case was never triggered. But this does not answer the question "how to get the limited photos collection" only – GrayFox Nov 05 '20 at 15:51
  • getting the limited photos collection, you have to manage yourself, like storing those photos in local DB, there is a well-written article, go through this https://www.wwdcnotes.com/notes/wwdc20/10641/ – Sheshnath Nov 06 '20 at 03:58
  • 1
    Okay but your solution implies to create our own photos picker to only display the limited photos collection. This is that exact behavior I want to get rid off by using the PHPickerViewController – GrayFox Nov 06 '20 at 12:05
  • Yes, eventually we also ended up using PHPickerViewController, previously we were using BSImagePicker library just to allow user selecting multiple images, now PHPickerViewController allow user selecting multiple images, besides you don't even need to ask gallery access permission – Sheshnath Nov 06 '20 at 12:28