Can anyone help me understand the difference between the following two lines:
[UIApplication.sharedApplication.delegate.window addSubview:myView];
[UIApplication.sharedApplication.keyWindow addSubview:myView];
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.
They could be the same on iOS. When they are different, usually you are presenting another window other than the app delegate's main window. Your app can have many windows but only the keyWindow
is the window that is visible on the screen and receiving events (example could be a UIAlert when visible and receiving events it is the keywindow) reference: https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/WindowAndScreenGuide/WindowScreenRolesinApp/WindowScreenRolesinApp.html
from the documentation:
UIApplication.sharedApplication.delegate.window
:The window to use when presenting a storyboard. This property contains the window used to present the app’s visual content on the device’s main screen.
i.e this is the property window
that you have in your AppDelegate.h
file.
UIApplication.sharedApplication.keyWindow
:This property holds the UIWindow object in the windows array that is most recently sent the makeKeyAndVisible message.
On iOS you call makeKeyAndVisible
in your AppDelegate.m
inside
application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
You made the created appDelegate window the keyWindow. Usually, bank apps switch the key window when the app is put in the background to protect the users sensitive information when the home button is doubled tapped and switch back to the main delegate window when the app is in foreground.
This answer is in collaboration with: @SipkeSchoorstra, @D-Mx and @andyDarwin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With