Last active
March 13, 2025 00:22
-
-
Save IanKeen/66d3ab351a73befc394eb266ac5c8fe8 to your computer and use it in GitHub Desktop.
Example main.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| import SwiftUI | |
| let isUITesting = /* your UI test detection here */ | |
| @main | |
| struct EntryPoint { | |
| static func main() { | |
| if isUITesting { | |
| UITestApp.main() | |
| } else if NSClassFromString("XCTestCase") == nil { | |
| MyApp.main() | |
| } else { | |
| TestApp.main() | |
| } | |
| } | |
| } | |
| struct MyApp: App { | |
| var body: some Scene { | |
| WindowGroup { | |
| //.. app here | |
| } | |
| } | |
| } | |
| struct UITestApp: App { | |
| var body: some Scene { | |
| WindowGroup { | |
| //.. prod app bootstrapped for UI tests | |
| } | |
| } | |
| } | |
| struct TestApp: App { | |
| var body: some Scene { | |
| WindowGroup { Text("Running tests...") } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| NSSetUncaughtExceptionHandler { print("🧨", $0) } | |
| let isUITesting = /* your UI test detection here */ | |
| let isUnitTesting = NSClassFromString("XCTest") != nil | |
| let noTests = !isUnitTesting && !isUITesting | |
| let delegate = isUITesting | |
| ? NSStringFromClass(UITestAppDelegate.self) | |
| : noTests ? NSStringFromClass(AppDelegate.self) | |
| : nil | |
| print("🚀 Using AppDelegate:", delegate as AnyObject) | |
| UIApplicationMain(CommandLine.argc, CommandLine.unsafeArgv, nil, delegate) |
Author
Author
The file needs to be called main.swift if you are using the UIApplicationMain approach. The EntryPoint approach can be anywhere as it will be found via the @main attribute.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The last step is to remove
@UIApplicationMainfrom your existing application delegate