Utilitiy methods for use with HotReloading or InjectionIII to make live code updates to fully functional SwiftUI Applictions.
Add this repo to your project and add the following to a source file.
@_exported import HotSwiftUI
This will make the .enableInjection() method on SwiftUI.View
used to erase their type available throughout the app along with the
global injectionObserver variable you can observe to force the
View to update when code has been injected. SwiftUI is very well
suited to injection as, provided you observe the injectionObserver
which has an @Published injection counter, you can rest assured
your views will update as required.
In short, modify all your SwiftUI View structs to look like this:
...
// use the new property wrapper
@ObserveInjection var redraw
var body: some View {
// Your SwiftUI code...
.enableInjection()
}You need to do this for all view properties you'd like to inject and have refesh on injection. This is a bit tedious but the InjectionIII or HotReloading app can make these changes automatically using the "Prepare Project" Menu Item. You can check in these changes as, in a "Release" build, these functions compile to a null operation.
There is a compatible version of this code along with ideas further developed for "hosting" UIViewControllers in the project https://github.com/krzysztofzablocki/Inject.