38

UIApplication has a method keyWindow, however if an alert view is showing then this returns the window of the alert view and not the main window of the application.

How can I get the app's main window?

Jonathan.
  • 53,997
  • 54
  • 186
  • 290

8 Answers8

59

The UIApplicationDelegate usually has a reference to the "main window":

[[[UIApplication sharedApplication] delegate] window];

Furthermore, UIApplication has an array of windows [[UIApplication sharedApplication] windows].

See the UIApplication Class Reference.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Espresso
  • 4,722
  • 1
  • 24
  • 33
12

I'm not 100% sure this works in every case but this should work:

UIWindow *mainWindow = [UIApplication sharedApplication].windows[0];

The windows are ordered back to front so the main window should always be at index 0.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 12
    You should call `[[[UIApplication sharedApplication] windows] firstObject]` to avoid an out of bounds exception if there are no windows (not like there would never be any windows, but it's a good habit to avoid those – michaellindahl Aug 15 '14 at 19:21
  • @michaellindahl even if 12 people agreed. I can't say it's a good habit 'to check something even if we don't know how it works. Even you wrote: "not like there would never be any windows". The best choice here is to write explicitly: "I'm sure there is a window index 0". It will lead you to a bounds exception. And it's Great! You can't win with a strategy as Michael suggested - it will lead to a mess with these necessary checkings. Who knows later if we need them. – Vlad Servitola Sep 19 '22 at 15:03
7

Swift 3.0 version of rmaddy's answer:

let window = UIApplication.shared.windows.first

I also should add that since iOS 8.0 UIAlertController has replaced UIAlertView and being a view controller you may no longer face the issue of new windows being created.

Community
  • 1
  • 1
fpg1503
  • 7,492
  • 6
  • 29
  • 49
3
UIApplication *application = [UIApplication sharedInstance];
NSarray *appWindows = [NSArray arrayWithArray:application.windows];
UIWindow *mainWindow = [appWindows objectAtIndex:0];

I am not sure but this might help.

RKY
  • 266
  • 1
  • 7
3

In Swift:

UIApplication.sharedApplication().delegate?.window
Glauco Neves
  • 3,483
  • 1
  • 24
  • 36
0

For me I was presenting a popViewController

self.presentViewController(popViewController, animated: true, completion: nil)

and then in the viewDidLoad() of this popViewController I was adding a subview, this causes the error in the console and a display bug. So I have to find another solution to make it work. Hope this helps.

AnthonyR
  • 3,485
  • 1
  • 18
  • 41
0

Swift 3

    if let window = NSApplication.shared().windows.first {
        // you can now modify window attributes
    }
Ted Lowery
  • 1,535
  • 2
  • 13
  • 8
  • He said `UIApplication` not `NSApplication`, it's tagged as iOS – fpg1503 Dec 27 '16 at 22:51
  • 2
    If I had a dollar for every time this has happened in reverse (people answering Cocoa questions with Cocoa Touch answers), I'd be a wealthy man. :) Cut Ted some slack. :) – Clifton Labrum Nov 10 '17 at 20:57
-2

Swift 3

class func sharedInstance() -> AppDelegate{
        return UIApplication.shared.delegate as! AppDelegate
    }
Ved Rauniyar
  • 1,539
  • 14
  • 21