Container Photo Picker
Limits the Photos picker to only show items in the specified container. In this case, to the user-created album "CustomAlbum".
Platform Compatibility
Tags
SwiftUI Code
@State private var selectedPhoto: PhotosPickerItem?
@State private var albumIdentifier: String?
var body: some View {
PhotosPicker("FooBar", selection: $selectedPhoto)
.photosPickerContainerIdentifier(albumIdentifier)
.disabled(albumIdentifier == nil)
.task { await loadAlbumIdentifier() }
}
private func loadAlbumIdentifier() async {
guard await PHPhotoLibrary.requestAuthorization(for: .readWrite) == .authorized else {
print("Full photo library access is required to find Custom Album.")
return
}
let options = PHFetchOptions()
options.predicate = NSPredicate(format: "title ==[c] %@", "CustomAlbum")
guard let album = PHAssetCollection.fetchAssetCollections(
with: .album,
subtype: .albumRegular,
options: options
).firstObject else {
print("Custom Album not found.")
return
}
albumIdentifier = album.localIdentifier
}