4

I am wondering why:

UIApplication.shared.windows.first { $0.isKeyWindow } 

always returns nil both in the simulator and on device. You can confirm this by putting a break point in viewDidLoad in any view controller and printing the object to the console.

po UIApplication.shared.windows.first { $0.isKeyWindow } 

The only way I can get the "keyWindow" is by getting

UIApplication.shared.windows.first

But there is no explicit mention that it is key. What if there are multiple instances of my app on iPad Split view?

Any ideas?

My thinking is, if there is only one window it's key by default and if you're running more than one window on iPad the one that is currently being interacted with will become the key window, however, I haven't tested this.

Daniel Storm
  • 15,870
  • 6
  • 74
  • 133
Adam Dahan
  • 51
  • 3

1 Answers1

-1

Have you tried this?

UIApplication.shared.windows.filter {$0.isKeyWindow}.first
Boris
  • 314
  • 5
  • 20