There is poor documentation about Prewarming:
Prewarming executes an app’s launch sequence up until, but not including, when main() calls UIApplicationMain(::::). https://developer.apple.com/documentation/uikit/app_and_environment/responding_to_the_launch_of_your_app/about_the_app_launch_sequence#3894431
But prewarming can call both willFinishLaunchingWithOptions and didFinishLaunchingWithOptions from my tests on my iPhone 13 with iOS 15.0.
I’ve created a test app which logs all calls to a string array:
main.swift
var logMessages = [String]()
func addLogMessage(_ text: String) {
let date = Date().formatted(date: .omitted, time: .standard)
logMessages.append("\(date): \(text)")
}
autoreleasepool {
addLogMessage("main.swift")
UIApplicationMain(CommandLine.argc, CommandLine.unsafeArgv, nil, NSStringFromClass(AppDelegate.self))
}
AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
override init() {
super.init()
addLogMessage("AppDelegate.init")
}
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
addLogMessage("willFinishLaunchingWithOptions")
return true
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
addLogMessage("didFinishLaunchingWithOptions")
return true
}
func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication) {
addLogMessage("applicationProtectedDataDidBecomeAvailable")
}
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
addLogMessage("configurationForConnecting")
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
…
}
SceneDelegate.swift
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow? {
didSet {
addLogMessage("SceneDelegate.didSet window")
}
}
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
addLogMessage("SceneDelegate.willConnectTo")
guard let _ = (scene as? UIWindowScene) else { return }
}
…
}
Then did the following:
- Launch the app
- Use it briefly
- Force quit the app
- Lock my device and leave it for ~30 minutes
- Unlock the device
- Launch the app again
My logs:
12:28:36 AM: main.swift
12:28:36 AM: AppDelegate.init
12:28:36 AM: willFinishLaunchingWithOptions
12:28:36 AM: didFinishLaunchingWithOptions
12:58:15 AM: applicationProtectedDataDidBecomeAvailable
12:58:15 AM: configurationForConnecting
12:58:15 AM: SceneDelegate.didSet window
12:58:15 AM: SceneDelegate.willConnectTo
12:58:15 AM: viewDidLoad
As you can see the system launched my app (prewarm) at 12:28:36 and held in background until my manual start at 12:58:15 and after gave access to protected data and loaded UI.