Hot Reloading a SwiftUI App

Photo by Yaoqi on Unsplash

Hot Reloading a SwiftUI App

Change code and instantly see the updates in the iOS simulator without compiling your SwiftUI app!

You need

  • Inject, a Swift Package created by Krzysztof Zabłocki
  • InjectionIII, a macOS application for the heavy lifting (i.e. watching for modified source code and interposing the new function as if it had bee compiled into the SwiftUI app)

You can read more about the Swift Package in the following article.

I also recommend reading the book "Swift Secrets" by John Holdsworth. He is the creator of InjectionIII (also known as "Injection for Xcode) and shares the the concept of how hot reloading for iOS development is possible in the first place!

There is plenty of reading material, so I want to show you hot reloading in action! I show you all the steps needed to instrument your SwiftUI app in my YouTube video.

  1. Add the Swift Package
  2. Import its module in the source file containing your SwiftUI view
  3. Add @ObservedObject private var iO = Inject.observer variable to your SwiftUI View
  4. call .enableInjection() at the end of your view's body definition

The video skips how to install InjectionIII.app (see here for details) but I'll show you how to use macOS application.

I also share some observations and tricks and tips!

You can find the test app on my GitHub and the commit history visualizes each step done for instrumenting the SwiftUI app for hot reloading.

Did you find this article valuable?

Support Marco Eidinger by becoming a sponsor. Any amount is appreciated!