What is the difference between UIApplication.sharedApplication.delegate.window and UIApplication.sharedApplication.keyWindow?

For most uses, they will be the same… but not always.

[UIApplication sharedApplication].keyWindow is the window which is currently being displayed on the device. This is normally your application’s window, but could be a system window.

[UIApplication sharedApplication].delegate.window is the window your application is expected to use.

Which one should be used? Well that all depends on context.

If you are updating part of your application, then you should add views to your application’s window. This is almost always what you want to do.

Personally, I always use [[UIApplication sharedApplication].delegate.window addSubview:view] or [self.view.window addSubView:view] (within UIViewController) when I need to add a view directly to the window.

There might be some times when you want to present a view to the window currently being displayed, regardless if the window belongs to your application or is some system window. I’ve not run into that situation.

Leave a Comment