Hot Reloading in iOS App Development

One of the things iOS developer spends a lot of time on is compiling and building the project. it is a huge time waster, especially when you just want to see some simple UI changes, such as text or color change. While SwiftUI supports preview in Xcode, when the project gets larger and more complicated, it became not reliable as well.

Luckily, there are some open source libraries that help to address this issue. They are InjectionIII and Inject.

Inject is a wrapper of InjectionIII to do most of the work and setup for you. It is suitable for you if your project is small & simple, however, it may not work well for project that has multiple packages or modules based on my experience. If you have used it well in your complicated project, feel free to let me know you how you do the integration, so that I can learn from you as well.

If anyone in your project wants to use injection, they only need to:

  • You must add “-Xlinker -interposable” (without the double quotes) to the “Other Linker Flags” of all targets in your project for the Debug configuration (qualified by the simulator SDK to avoid complications with bitcode), refer to InjectionForXcode documentation if you run into any issues
  • Download newest version of Xcode Injection from it’s GitHub Page
  • Unpack it and place under /Applications
  • Make sure that the Xcode version you are using to compile our projects is under the default location: /Applications/Xcode.app
  • Run the injection application
  • Select open project / open recent from it’s menu and pick the right workspace file you are using

After choosing the project in Injection app, launch the app

  • If everything is configured correctly you should see similar log in the console:
💉 InjectionIII connected /Users/ykpoh/work/YourProject/App.xcworkspace
💉 Watching files under /Users/ykpoh/work/YourProject

Customisation

If you have tried the setup above and doesn’t work straight away, you may need to do some customisation for it to work. Instead of relying on Inject, you can import InjectionIII and do the work yourself. One way is to add notification observer in the place where you load your view controller, so that it will trigger the customisation code and reload the view controller.

NotificationCenter.default.addObserver(self,
    selector: #selector(configureView),
    name: Notification.Name("INJECTION_BUNDLE_NOTIFICATION"), object: nil)
@objc func configureView() {
    // reload view controller code
    let vc = SampleViewController()
    navigationController.setViewControllers([vc], animated: false)
}

After you have opened InjectionIII app in your desktop and have selected your project to listen, when you save any file, InjectionIII will send out INJECTION_BUNDLE_NOTIFICATION, the code in configureView function will get triggered and run accordingly. The code above is just a sample, you can design based on your use case.

Hope this article helps you in your iOS journey!

Leave a Comment

Your email address will not be published. Required fields are marked *