1

In the UIApplicationDelegate's application:didFinishLaunchingWithOptions: I have the following code:

NSLog(@"%d\n", [UIApplication sharedApplication].windows.count);

self.mainWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

NSLog(@"%d\n", [UIApplication sharedApplication].windows.count);

(mainWindow is a @property declared in the header)

The first NSLog prints out 0 while the second prints 1.

How UIApplication knows that a window is created and initialized in the application?

JAL
  • 41,701
  • 23
  • 172
  • 300
Viktor Simkó
  • 2,607
  • 16
  • 22

3 Answers3

2

While we don't have the source code to be 100% sure, it would seem that the initializer of UIWindow is written to tell UIApplication that it has been created. It's simply part of Apple's implementation of the two classes.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 2
    More specifically, the `UIWindow` class keeps an array of all created windows which all windows add themselves to in their initializers and `[UIApplication sharedApplication] windows]` calls a class method on `UIWindow` that filters and returns that array. – dan Jun 06 '16 at 16:45
  • @dan - How do you know this? None of that is in the public API. – rmaddy Jun 06 '16 at 16:47
  • I disassembled UIKit with Hopper because I was curious about what it did specifically. Obviously it's an implementation detail which can change at any time and can't be relied on. – dan Jun 06 '16 at 16:49
  • @dan Nice. It makes sense. I was just curious how you knew. – rmaddy Jun 06 '16 at 16:51
  • @dan Check [my answer](http://stackoverflow.com/a/37662901/2415822) with regard to private methods. – JAL Jun 06 '16 at 16:53
  • @dan An interesting aspect of this is that you can swizzle `UIWindow` `init` and `dealloc` methods and gain access to the system windows (status bar & keyboard, which were hidden from `UIApplication.windows` in iOS 7). This can be used to take screenshot of your app programatically, including status bar & keyboard. Not for production of course. – Sulthan Jun 06 '16 at 17:02
  • @Sulthan You can also get those windows by calling the private `+[UIWindow allWindowsIncludingInternalWindows:onlyVisibleWindows:]` method passing `YES` to the first parameter. – dan Jun 06 '16 at 17:15
1

If you're looking for a specific function (to hook, or are just curious), most of the application setup in UIWindow is performed in the - (void)_createContextIfNecessaryForCurrentApplicationState; method and the NSISEngineDelegate protocol methods.

JAL
  • 41,701
  • 23
  • 172
  • 300
0

Apple doc says

When an app is launched, the system calls the UIApplicationMain function; among its other tasks, this function creates a singleton UIApplication object. Thereafter you access the object by calling the sharedApplication class method. A major role of your app’s application object is to handle the initial routing of incoming user events. It dispatches action messages forwarded to it by control objects (instances of the UIControl class) to appropriate target objects. The application object maintains a list of open windows (UIWindow objects) and through those can retrieve any of the app’s UIView objects.

Cjay
  • 1,093
  • 6
  • 11