Hot Reload for SwiftUI and UIKit

Rafał Prążyński
Nerd For Tech

--

Hello there!

Today I want present to you really hot topic and something what is in my opinion the best third party library I’ve ever seen for iOS!

The third party library that I want to present is Inject.

It is a tool made by Krzysztof Zabłocki and thanks to him we have the possibility to see changes on UI when we change code concerning visual format without recompiling it, all of that in the running app.

Something that is available on other platforms finally comes to us!

Let’s do the magic in both ways: SwiftUI and UIKit.

Besides Inject we will need to use Injectionlll

For both: SwiftUI and UIKit, you’ll need to make the same configuration which is:

And to SPM:

Next go to Build Settings, search for Other Linker Flags and add:

  • -Xlinker
  • -interposable

SwiftUI:

Now in target View file do similar changes like here:

  1. Import Inject
  2. Add property for listening to notification when changes occur
  3. Calling this method will finish with load proper Bundle in InjectionIII depending on what platform we run the code.

FYI: After making changes in code remember to SAVE the file (cmd + s) or wait until IDE saves it automatically.

Now, start project and you should see magic like here:

UIKit:

In UIKit we can add tracking for object inherited from UIViewController or UIView.

The setup is straightforward for both cases compared to SwiftUI configuration:

UIViewController:

Result:

ViewController.swift file

UIView:

Result:

SomeView.swift file

Thanks to Inject, our changes in code can be visible on the fly! It is really convenient considering how much time is sometimes needed to rebuild the whole project when we want to make only small changes to a few lines of code in a single file.

Thank you for taking the time to read up to this point.

Good luck!

Full code: https://github.com/Rafal-Prazynski/Inject_medium

P.S.:

If you encounter undefined FSEventStream* symbols, this is due to using a macOS api in the simulator and you should find this goes away if add the following additional "Other Linker Flags":

  • -Xlinker
  • -undefined
  • -Xlinker
  • dynamic_lookup

--

--