ItemIdentifiers when selecting multiple photos with PhotosPicker

The problem: In iOS 16.2, selecting multiple photos with PhotosPicker does not seem to allow me to access the selected photos' local identifiers. More accurately, the attribute itemItentifier is alway nil. In a single-selection mode, the itemIdentifier populates normally.

Example

I am selecting multiple photos with PhotosPicker as follows:

        .photosPicker(isPresented: $showMultiplePhotoPicker,
                      selection: $photoSelections,
                      matching: .images,
                      photoLibrary: .shared()
        )

and then observing changes in photoSelections as follows:

        .onChange(of: photoSelections) { _ in
            for selection in photoSelections {
                print("Selection: \(selection)")
                print("Item identifier: \(selection.itemIdentifier)")

What gets printed in the console:

Selection: PhotosPickerItem(_itemIdentifier: Optional("B84E8479-475C-4727-A4A4-B77AA9980897/L0/001"), _shouldExposeItemIdentifier: false, _supportedContentTypes: [<_UTCoreType 0x1b9bdda00> public.jpeg (not dynamic, declared)], _itemProvider: <PUPhotosFileProviderItemProvider: 0x60000112c090> {types = (
    "public.jpeg"
)})
Item identifier: nil

So while the PhotosPickerItem has a value for _itemIdentifier, it's replaced by nil when accessing it via .itemIdentifier. The culprit here, I think, is _shouldExposeItemIdentifier: false: this value is true in a single-selection mode.

Any thought on this would be highly appreciated!

Answered by PixelPenguin in 751772022

Nevermind. I was accidentally calling PhotosPicker in two different ways, and the one that was actually run didn't specify photoLibrary: .shared(). As the documentation for ItemIdentifier says, "This value is nil if you create a Photos picker without a photo library."

Accepted Answer

Nevermind. I was accidentally calling PhotosPicker in two different ways, and the one that was actually run didn't specify photoLibrary: .shared(). As the documentation for ItemIdentifier says, "This value is nil if you create a Photos picker without a photo library."

ItemIdentifiers when selecting multiple photos with PhotosPicker
 
 
Q