keyWindow property was deprecated in iOS 13 because it doesn’t work correctly with multiple scenes. For single-scene apps, use UIApplication.shared.windows.first(where: \{$0.isKeyWindow\}). For multi-scene support, filter UIApplication.shared.connectedScenes for foreground active UIWindowScene instances and take the first key window.Why this happens
Apple deprecated UIApplication.shared.keyWindow in iOS 13.0 with the introduction of multiple scene support (iPadOS multi-window, Catalyst, and later visionOS). The deprecation warning states:
'keyWindow' was deprecated in iOS 13.0: Should not be used for applications that support multiple scenes as it returns a key window across all connected scenes
In the old single-window model, there was exactly one key window per app. With scenes, each UIWindowScene manages its own window hierarchy, and the concept of a single “key window for the whole app” no longer maps cleanly. The legacy property still returns a key window, but it may belong to a background scene or the wrong scene entirely, leading to presentation crashes or UI appearing on the wrong display.
If your app opts out of multiple scenes (by not including UIApplicationSupportsMultipleScenes in Info.plist or by not adopting UISceneDelegate), the old property continues to work. However, Apple encourages migration to the scene-based APIs for forward compatibility.
How to fix it
1. Simple fix for single-scene apps (recommended)
If your app runs one scene at a time — the vast majority of iPhone apps and many iPad apps — the cleanest replacement is:
let keyWindow = UIApplication.shared.windows.first { $0.isKeyWindow }
UIApplication.shared.windows returns every window belonging to the current scene (or the only scene). Filtering for isKeyWindow gives you the exact window that currently receives keyboard and touch events. This one-liner replaces the old keyWindow call with zero scene-handling boilerplate.
Usage example — presenting an alert from anywhere:
func showAlert(title: String, message: String) {
guard let window = UIApplication.shared.windows.first(where: { $0.isKeyWindow }),
let rootVC = window.rootViewController else { return }
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
rootVC.present(alert, animated: true)
}
Note:
UIApplication.shared.windowsis itself deprecated in iOS 15 but remains available and safe for single-scene apps. See Fix 2 for the fully future-proof approach.
2. Fully scene-aware fix (iOS 13+, multi-scene ready)
When your app does support multiple scenes (iPad split view, external display, Mac Catalyst), you must ask the foreground active scene for its key window:
func currentKeyWindow() -> UIWindow? {
UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.filter { $0.activationState == .foregroundActive }
.first?.windows
.first { $0.isKeyWindow }
}
Step-by-step breakdown:
connectedScenes— all scenes the app currently holds (foreground, background, unattached).compactMap { $0 as? UIWindowScene }— keep only window scenes; dropUIScenesubclasses that don’t manage windows.filter { $0.activationState == .foregroundActive }— select the scene the user is actively looking at..first?.windows— take that scene’s window array..first { $0.isKeyWindow }— return the key window inside that scene.
Presenting from a background task (e.g., CloudKit sync error):
func presentCloudKitError(_ error: Error) {
guard let window = currentKeyWindow(),
let rootVC = window.rootViewController else { return }
let alert = UIAlertController(title: "iCloud Sync",
message: error.localizedDescription,
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Retry", style: .default) { _ in
// retry logic
})
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel))
rootVC.present(alert, animated: true)
}
3. SwiftUI alternative — use the view hierarchy
If the presenting code lives inside a SwiftUI view, avoid UIWindow entirely. Prefer SwiftUI’s own presentation APIs:
struct ContentView: View {
@State private var showAlert = false
var body: some View {
Text("Hello")
.alert("iCloud Issue", isPresented: $showAlert) {
Button("OK") { }
} message: {
Text("Please check your iCloud settings.")
}
}
}
For UIKit-hosted SwiftUI (e.g., UIHostingController), you can still reach the window via the view’s window property:
// Inside a UIView or UIViewController
if let window = self.view.window ?? self.window {
// window is the correct UIWindow for this scene
}
How to prevent it
- Audit deprecated APIs — Run Product → Analyze (⇧⌘B) in Xcode; it flags
keyWindowandwindowsusage. - Adopt
UISceneDelegate— New projects use scenes by default. Keep the scene lifecycle methods (scene(_:willConnectTo:options:)) for window setup. - Test on iPad with Stage Manager — Verify multi-window behavior: open two windows of your app, trigger the alert from each, and confirm it appears in the correct window.
- Wrap window access in a helper — Centralize the logic in a
WindowProvidersingleton or environment value so future API changes only require one edit. - Migrate legacy AppDelegate code — Move window-level work (root VC setup, appearance proxies) into
SceneDelegateor the SwiftUIAppentry point.
FAQ
Is it safe to keep using keyWindow if I don’t support multiple windows?
Yes. If your Info.plist lacks UIApplicationSupportsMultipleScenes (or you explicitly set it to NO), the app runs in single-scene compatibility mode. keyWindow will return the correct window and the deprecation warning is merely advisory. However, the property may be removed in a future major iOS release, so migration is still recommended.
Why does UIApplication.shared.windows show a deprecation warning in iOS 15?
Apple deprecated the windows collection on UIApplication for the same reason: it conflates windows across scenes. In a single-scene app the array contains only that scene’s windows, so it works, but the compiler warns you anyway. Use the connectedScenes approach (Fix 2) to silence the warning and gain true multi-scene correctness.
How do I present an alert when there’s no visible window (e.g., at launch)?
During early launch, no scene may be foregroundActive yet. Defer presentation until sceneDidBecomeActive(_:) in your UISceneDelegate, or schedule the work with DispatchQueue.main.async after the first frame. In SwiftUI, drive presentation from @State that flips once the view appears.
Need to share the key window across SwiftUI views? Wrap the helper in an EnvironmentKey and inject it at the App level for clean, testable access.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.
