Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Created October 31, 2023 01:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JadenGeller/afb907745865052ed59c53b5ca9f8671 to your computer and use it in GitHub Desktop.
Save JadenGeller/afb907745865052ed59c53b5ca9f8671 to your computer and use it in GitHub Desktop.
SwiftUI view for displaying limited photo library picker
import SwiftUI
import PhotosUI
struct LimitedLibraryPicker: UIViewControllerRepresentable {
@Binding var isPresented: Bool
func makeUIViewController(context: Context) -> UIViewController {
.init()
}
class Coordinator {
var isPresented = false
}
func makeCoordinator() -> Coordinator {
.init()
}
func updateUIViewController(_ controller: UIViewController, context: Context) {
if isPresented, !context.coordinator.isPresented {
Task {
context.coordinator.isPresented = true
await PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: controller)
context.coordinator.isPresented = false
await MainActor.run { isPresented = false }
}
}
else if !isPresented, context.coordinator.isPresented {
Task {
await MainActor.run { isPresented = true }
}
}
}
}
extension View {
func limitedLibraryPicker(isPresented: Binding<Bool>) -> some View {
overlay(LimitedLibraryPicker(isPresented: isPresented))
}
}
@gavinjerman
Copy link

Just what I was looking for, unfortunately when debugging the app under Xcode 15.1 I get the following warning when the picker is displayed:

Presenting view controller <PHPickerViewController: 0x103240080> from detached
view controller <UIViewController: 0x1036bcca0> is not supported, and may result
in incorrect safe area insets and a corrupt root presentation. Make sure 
<UIViewController: 0x1036bcca0> is in the view controller hierarchy before presenting
from it. Will become a hard exception in a future release.

I have the picker attached to an HStack inside a Form.

So, unfortunately, might not be the solution I was looking for.

Cheers.

Gavin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment