I need to run some code directly when the user invokes the PhotosPicker. I have tried .onAppear and .onTapGesture, but those methods never gets triggered when tapping the button that opens the PhotosPicker-view.
PhotosPicker(selection: $selectedItem, matching: .any(of: [.images, .not(.livePhotos)])) {
Image(systemName: "photo")
.controlSize(.large)
.foregroundColor(.white)
}
.onTapGesture {
print("--- This is never executed ---")
}
.onChange(of: selectedItem) { newItem in
Task {
if let data = try? await newItem?.loadTransferable(type: Data.self) {
selectedPhotoData = data
}
}
}
You should use the
ViewModifier
viaphotosPicker(ispresented:selection:matching:preferreditemencoding:)
instead of theView
version of Photos Picker.That way, you can create a Button instead, which: