Hot Reloading a SceneDelegate in Swift

I told you about Hot Reloading a SwiftUI App in my previous blog post.In this blog post, I will tell you how to inject code that is not bound to a UI representation. Example: you need to change code in your scene delegate when testing the handling of c…


This content originally appeared on Level Up Coding - Medium and was authored by Marco Eidinger

I told you about Hot Reloading a SwiftUI App in my previous blog post.

In this blog post, I will tell you how to inject code that is not bound to a UI representation. Example: you need to change code in your scene delegate when testing the handling of custom URLs in a UIKit-based app.

You need the same tooling:

  • 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 been compiled into the SwiftUI app)

I assume you made the necessary configurations in the Build Settings of your App (-Xlinker -interposable) and that you run the InjectIII.app on your Mac.

The critical difference is that we don’t use Inject.ViewControllerHost(...) or Inject.ViewHost(...). Instead we call the lazy property Inject.load in the AppDelegate so that InjectIII.app will watch for file changes right from the application start.

import UIKit
import Inject
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Inject.load // connects running iOS simulator with InjectIII.app
return true
}
// ...
}

That’s it !

In my video, I also prove that changes to SceneDelegate.swift will be injected and eventually executed when called by the iOS lifecycle handling.

For my video I registered a URL scheme in the Info.plist.

I added the following function to the SceneDelegate class before running the app.

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
for context in URLContexts {
print("url: \(context.url.absoluteURL)")
print("scheme: \(String(describing: context.url.scheme))")
print("host: \(String(describing: context.url.host))")
print("path: \(context.url.path)")
print("components: \(context.url.pathComponents)")
}
}

Once the app starts running in the iOS simulator, I change the implementation of scene(_:openURLContexts:)

Using the terminal command xcrun simctl openurl booted <myCustomURLScheme>, I invoke a custom URL in the running iOS simulator, and I witness that the injected code gets executed.

Originally published at https://blog.eidinger.info.

Level Up Coding

Thanks for being a part of our community! Before you go:


Hot Reloading a SceneDelegate in Swift was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Marco Eidinger


Print Share Comment Cite Upload Translate Updates
APA

Marco Eidinger | Sciencx (2022-07-19T14:58:05+00:00) Hot Reloading a SceneDelegate in Swift. Retrieved from https://www.scien.cx/2022/07/19/hot-reloading-a-scenedelegate-in-swift/

MLA
" » Hot Reloading a SceneDelegate in Swift." Marco Eidinger | Sciencx - Tuesday July 19, 2022, https://www.scien.cx/2022/07/19/hot-reloading-a-scenedelegate-in-swift/
HARVARD
Marco Eidinger | Sciencx Tuesday July 19, 2022 » Hot Reloading a SceneDelegate in Swift., viewed ,<https://www.scien.cx/2022/07/19/hot-reloading-a-scenedelegate-in-swift/>
VANCOUVER
Marco Eidinger | Sciencx - » Hot Reloading a SceneDelegate in Swift. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/19/hot-reloading-a-scenedelegate-in-swift/
CHICAGO
" » Hot Reloading a SceneDelegate in Swift." Marco Eidinger | Sciencx - Accessed . https://www.scien.cx/2022/07/19/hot-reloading-a-scenedelegate-in-swift/
IEEE
" » Hot Reloading a SceneDelegate in Swift." Marco Eidinger | Sciencx [Online]. Available: https://www.scien.cx/2022/07/19/hot-reloading-a-scenedelegate-in-swift/. [Accessed: ]
rf:citation
» Hot Reloading a SceneDelegate in Swift | Marco Eidinger | Sciencx | https://www.scien.cx/2022/07/19/hot-reloading-a-scenedelegate-in-swift/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.