Get Application's main window Get Application's main window ios ios

Get Application's main window


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.


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.


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.


matomo