Skip to content

Instantly share code, notes, and snippets.

@MMnasrabadi
Forked from IanKeen/EntryPoint.swift
Created July 8, 2024 06:25
Show Gist options
  • Save MMnasrabadi/5db56316285c35af5cf4432b1c0d6f65 to your computer and use it in GitHub Desktop.
Save MMnasrabadi/5db56316285c35af5cf4432b1c0d6f65 to your computer and use it in GitHub Desktop.
Example main.swift
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...") }
}
}
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)
let argc = CommandLine.argc
let argv = UnsafeMutableRawPointer(CommandLine.unsafeArgv)
.bindMemory(to: UnsafeMutablePointer<Int8>?.self, capacity: Int(CommandLine.argc))
UIApplicationMain(argc, argv, nil, delegate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment