如何在 iOS 14 中使用 PHAuthorizationStatusLimited

Mr.*_*ity 6 ios14 phpickerviewcontroller

为了获取照片的创建日期,所以在显示 PHPickerViewController 之前使用 requestAuthorizationForAccessLevel。

    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];
                });
            }
    }];
Run Code Online (Sandbox Code Playgroud)

尽管状态为 .limited,但 iOS 14 仍然显示所有图像。

如何使用 PHPickerViewController 只获取有限的照片?

she*_*ath 6

因此,iOS 14 中发生了一些变化,让我们一步一步来看看

1.如何读取PHPhotoLibrary访问权限状态

老的

let status = PHPhotoLibrary.authorizationStatus()
Run Code Online (Sandbox Code Playgroud)

新的

let status = PHPhotoLibrary.authorizationStatus(for: .readWrite)
Run Code Online (Sandbox Code Playgroud)

2.如何申请PHPhotoLibrary访问权限

老的

PHPhotoLibrary.requestAuthorization { status in
 //your code               
 }
Run Code Online (Sandbox Code Playgroud)

新的

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

如果用户授予您有限的权限,您有责任像下面的示例代码一样显示图库

if status == .limited {
     PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)
}
Run Code Online (Sandbox Code Playgroud)

当您展示LimitedLibraryPicker 时,从上一个会话中选择的图像将被标记为选中,并且屏幕顶部会显示一条消息-“选择更多照片或取消选择以删除访问权限

在此处输入图片说明

如果用户授予您有限的访问权限,您仍然使用 UIImagePickerController 或第三方库(如 BSImagePicker)展示普通图库,即使您可以选择并导入到您的应用程序,也会显示包含所有图片的图库,但在 Xcode 12 控制台中它会显示警告如下

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}
Run Code Online (Sandbox Code Playgroud)

  • 太棒了,这真的很有帮助,我不知道新的 .readWrite 授权。没有它,.limited 案例永远不会被触发。但这并不能回答“如何获得有限的照片集”的问题 (2认同)