It’s still a bit unclear how to present old UIKit ViewControllers for example a UIImagePickerController with his delegate methods in the current SwiftUI examples. I was hoping to give some insight to developers having the same problem. I don’t know if it’s best practice but let’s start creating an UIImagePickerController in SwiftUI.
So we conform to the UIViewControllerRepresentable protocol by having an makeUI and updateUI method which supports the UIImagePickerController which I want to show. This is a good start but we need to present this in a way SwiftUI handles it. This is actually really simple…. place it in a view which you present like every other view.
But you’re used to handling your delegate and in other cases maybe you will have a datasource as well. We will need to extend the previously created TestImagePickerController struct with a Coordinator class which will implement the necessary delegate, datasource or other classes. The whole struct will look like this.
SwiftUI calls this makeCoordinator()method before makeUIViewController(context:), so that you have access to the coordinator object when configuring your view controller.You can use this coordinator to implement common Cocoa patterns, such as delegates, data sources, and responding to user events via target-action.
You can also check the Interfacing with UIKit tutorial from Apple which does exactly the same with an UIPageViewController but it doesn’t mention how to present it or show any other similar example.
Happy SwiftUI coding!