Mr.Laity
Mr.Laity

Reputation: 133

How to use PHAuthorizationStatusLimited in iOS 14

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?

Upvotes: 8

Views: 8382

Answers (1)

Sheshnath
Sheshnath

Reputation: 3393

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}

Upvotes: 9

Related Questions