swift5如何在用户授予有限访问权限以选择某些照片后仅显示有限的照片库

Posted

技术标签:

【中文标题】swift5如何在用户授予有限访问权限以选择某些照片后仅显示有限的照片库【英文标题】:swift5 how to only display limited photo library after user has granted limited access to select some photos 【发布时间】:2020-11-07 05:56:08 【问题描述】:

在 WWDC2020 中,引入了新的 phpickerViewController 和新的 PHPhotoLibrary.authorizationStatus(limited)。但我遇到了以下问题:

    当用户点击一个按钮以显示苹果的多个图像选择器并将 requestAuthorization 显示为代码时:

     let requiredAccessLevel: PHAccessLevel = .readWrite
     PHPhotoLibrary.requestAuthorization(for: requiredAccessLevel)  (authorizationStatus) in
           switch authorizationStatus 
           case .authorized:
               DispatchQueue.main.async 
                   self.presentImagePicker()
                
           case .limited:
               DispatchQueue.main.async 
                   self.presentImagePicker()
                
           default:
               break
           
    
    

    self.presentImagePicker() 函数:

     func presentImagePicker() 
     var configuration = PHPickerConfiguration(photoLibrary: .shared())
     configuration.filter = .images
     configuration.selectionLimit = self.imageCountMax - self.images.count
     let picker = PHPickerViewController(configuration: configuration)
     picker.delegate = self
    
     let accessLevel: PHAccessLevel = .readWrite
     let authorizationStatus = PHPhotoLibrary.authorizationStatus(for: accessLevel)
    
     switch authorizationStatus 
     case .authorized:
         DispatchQueue.main.async 
    
             self.present(picker, animated: true)
         
     case .limited:
         DispatchQueue.main.async 
             // Here I don't know how to display only limited photo library to users (after user has selected some photos through the limited access)
         
     default:
         break
     
    

我的问题:请看代码2,case .limited: DispatchQueue.main.async ,我想我应该把有限的照片库放在这个块中,但我不知道如何只显示有限的照片库用户。

【问题讨论】:

【参考方案1】:

您可以将这个PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self) 方法用于.limited

DispatchQueue.main.async 
         // Here I don't know how to display only limited photo library to users (after user has selected some photos through the limited access)
        PHPhotoLibrary.shared().register(self)
        PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)
     

然后你必须使用委托方法来获取更新的图像。

func photoLibraryDidChange(_ changeInstance: PHChange) 
        let fetchOptions = PHFetchOptions()
        self.allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
    

【讨论】:

我遇到了同样的问题。您能否解释一下您是如何声明 allPhotos 的? @Quailcreek : var allPhotos: PHFetchResult!

以上是关于swift5如何在用户授予有限访问权限以选择某些照片后仅显示有限的照片库的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Appsync 在 cognito 中授予对某些用户组的访问权限

MySql 授予用户权限

创建一个IAM用户以授予执行在AWS EC2中某些任务的权限

授予 Windows 2012 R2 上某些计算机的管理员访问权限

如何仅将 EJB 角色授予某些实体

如何授予用户级别访问私有 GitLab 存储库的权限?