While I was working on a mixed UIKit and SwiftUI project, I needed a way to access the UIHostingController within the SwiftUI view so that I could use it for interacting with other UIKit methods. This blog post tackles the problem and provides a simple solution how to implement it.
The approach we are taking is using the SwiftUI environment and inserting an object into the environment, which then keeps a weak reference to the view controller hosting the SwiftUI view. Using the SwiftUI view environment has a benefit of allowing multiple other SwiftUI views within the hierarchy to use it as well. In the end, we would like to write something like this:
In the snippet above, we use a custom
embeddedInHostingController() function which inserts a new
ViewControllerProvider type the to the environment. Let’s take a closer look how this function and type are implemented.
ViewControllerProvider class keeps a weak reference to the view controller. Since
UIHostingController is a subclass of
UIViewController we can just use
UIViewController as a type. The embedded function creates an instance of the provider and a hosting controller, inserts the provider into the SwiftUI view environment and then sets the weak property which we can access later.