PHPickerViewController allows access to copies of photo library assets as well as returning PHAssets in the results. To get PHAssets instead of file copies, I do:
let photolibrary = PHPhotoLibrary.shared()
var configuration = PHPickerConfiguration(photoLibrary: photolibrary)
configuration.filter = .videos
configuration.selectionLimit = 0
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
self.present(picker, animated: true, completion: nil)
And then,
//MARK:- PHPickerViewController delegate
@available(iOS 14, *)
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true) {
let identifiers:[String] = results.compactMap(\.assetIdentifier)
let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: identifiers, options: nil)
NSLog("\(identifiers), \(fetchResult)")
}
}
But the problem is once the photo picker is dismissed, it prompts for Photo Library access which is confusing and since the user anyways implicitly gave access to the selected assets in PHPickerViewController, PHPhotoLibrary should load those assets directly. Is there anyway to avoid the Photo library permission?
Yes, sort of:
Change
PHPickerConfiguration(photoLibrary: photolibrary)
toPHPickerConfiguration()
Do not use the
assetIdentifier
to return to the photo library (it will benil
anyway)In that case, the user can give you image data but that's all. The picker is out-of-process and no data from the library itself is really coming across, just a mere image that the user has explicitly selected.
However, if your goal really is to return to the photo library and obtain the PHAsset, then you must have permission, as you now are indeed attempting to probe the photo library behind the scenes within your app.
What I do, when my app depends on PHAsset information, is to ask for photo library permission (if needed) before presenting the picker, and I don't present the picker if I can't get permission. So, the user taps a button, I discover we have no permission, I ask for permission, we get it (let's say), and I present the picker (doing the asynchronous dance, because the permission arrives asynchronously). Looks great.
By the way, this is also just as true with UIImagePickerController.