Created
October 31, 2023 01:00
-
-
Save JadenGeller/afb907745865052ed59c53b5ca9f8671 to your computer and use it in GitHub Desktop.
SwiftUI view for displaying limited photo library picker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
I have the picker attached to an HStack inside a Form.
So, unfortunately, might not be the solution I was looking for.
Cheers.
Gavin