How to use PHAuthorizationStatusLimited in iOS 14

夙愿已清 提交于 2021-02-04 19:46:16

问题


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?


回答1:


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"

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}


来源:https://stackoverflow.com/questions/63822338/how-to-use-phauthorizationstatuslimited-in-ios-14

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!