36

In Objective-C in the viewDidLoad method of a UIViewController I do this to get the keyWindow reference in my iOS app:

 UIWindow * keyWindow = [[UIApplication sharedApplication] keyWindow];

So, I am porting this view controller into Swift and I do this one the viewDidLoad call:

let window = UIApplication.sharedApplication().keyWindow

I am trying to understand why the window is nil. When I put a breakpoint right after that line I then inspect window in the console and I get this:

(lldb) po window
nil

How can I get window to be a valid reference to the keyWindow of my application in swift?

zumzum
  • 17,984
  • 26
  • 111
  • 172
  • 1
    `let window: UIWindow? = UIApplication.sharedApplication().windows[0] as? UIWindow` might present what you look for... or since `func viewDidAppear(_ animated: Bool)` (until your view is visible!) you have direct access to the window via `self.view.window`, maybe that is what you look for... – holex Jun 08 '15 at 15:11

7 Answers7

42

I came to this question when I was searching for getting window in swift. If you want to get window instead of keyWindow, try this (Swift 2):

if let app = UIApplication.sharedApplication().delegate as? AppDelegate, let window = app.window {
    MBProgressHUD.show(text, view:window)
}

Updated for Swift 3: (Thanks @Trevor)

if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
    MBProgressHUD.show(text, view:window)
}
superarts.org
  • 7,009
  • 1
  • 58
  • 44
  • Thanks, :) it saves my time. – Gautam Sareriya May 19 '16 at 04:36
  • But window.keyWindow will be false?! let windowFirst = NSApp.windows.first print(windowFirst!.keyWindow) – allenlinli Aug 05 '16 at 01:04
  • @allenlinli I think the discussion is around `iOS`, not sure about `macOS`. – superarts.org Nov 02 '17 at 19:17
  • What if I am using this code in pod file , and it doesnt see AppDelegate file. What should I do? – Yestay Muratov Nov 21 '18 at 08:27
  • @YestayMuratov I'm not sure if I'm following, but I'll try. In general, you can still get `UIApplication.shared.delegate` from a shared component, but you'll not be able to know the class `AppDelegate`. One thing you can do is create a `MyPodAppDelegateProtocol` and use the interface in your pod, and in the main app, of course `AppDelegate` has to confirm to this protocol. But it would be good for you to ask this question separately, as the approach above doesn't sound like a good design, as it violates multiple software engineer principles, especially those about dependency. – superarts.org Nov 21 '18 at 17:34
27

Swift 4 simply has UIApplication.shared.keyWindow property, no casting necessary.

Note that iOS 13/iPadOS introduces UIScenes and explicit support for multiple windows, and thus deprecates the concept of keyWindow as it is no longer valid.

This question has an overview how to get access to scene based windows.

juhan_h
  • 3,965
  • 4
  • 29
  • 35
  • 4
    `keyWindow` is deprecated in iOS 13 https://developer.apple.com/documentation/uikit/uiapplication/1622924-keywindow – Xavier L. Aug 05 '19 at 00:57
17

Swift 5.1

Works for me

If you are also looking for a way to deal with foreground and background modes you should try this

UIApplication.shared.windows.first(where: { $0.isKeyWindow })
nomnom
  • 936
  • 10
  • 21
4

Updating superarts.org's answer for Swift 3:

if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window { 
      MBProgressHUD.show(text, view: window)
}
rmooney
  • 6,123
  • 3
  • 29
  • 29
4

The easiest way to do that is:

in Objective-C

[UIApplication sharedApplication].windows.firstObject

in Swift

UIApplication.shared.windows.first!

Note that this works only if you do not support multiple windows.

3

The key window must not yet be set at the time of viewDidLoad. Your code works fine in viewDidAppear. Alternately, you can get the windows array in viewDidLoad, and get one (if there's more than one) of the windows from that array.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
2

Don't call window in didLoad function call the keyWindow in viewDidAppear function of ViewController

Irshad Ahmad
  • 1,363
  • 11
  • 17