How to get Deprecated keyWindow in Swift
byNote: Discussion in this post assumes we are considering single window based scenario.
Sometimes I need to access the main window of my app to add some temporary views to it - making it appear forefront no matter at what depth of user’s navigation-flow I am in.
From old days of single window based app we have been using very convenient property handed to us: keyWindow
This property holds the UIWindow object in the windows array that is most recently sent the makeKeyAndVisible() message.
guard let window = UIApplication.shared.keyWindow else {return}
It has been long time. Now this has been deprecated from iOS 13 onwards.
We have our windows accessible from windows array:
let window = UIApplication.shared.windows[0]
A free assumption would be that first window in array would be the main window of the app - being the root. We want the root window so that any view being added be directly on top for our given purpose here. It worked good until unexpected happened. First object did not give UIWindow but UITextEffectsWindow. I could not add my view on the top anymore - adding to UITextEffectsWindow didn’t work for curiosity.