Search code examples
swiftphotokit

swift5 how to only display limited photo library after user has granted limited access to select some photos


In WWDC2020, new PHPickerViewController and new PHPhotoLibrary.authorizationStatus(limited) were introduced. But I got below issue:

  1. when user tapped a button to show the apple's multiple images picker and display the requestAuthorization as code:

     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
           }
    }
    
  2. self.presentImagePicker() functions:

     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
     }
    

    }

my issue: please see code 2, case .limited: DispatchQueue.main.async { }, I think I should put the limited photo library in this block, but I don't know how to display only limited photo library to users.


Solution

  • You can use this PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self) method into .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)
         }
    

    and after that you have to use the delegate method for get the updated images.

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