[UIApplication sharedApplication
在项目开发过程中经常有需要在window上添加各种文本提示框,今天我们就有这么一个需求,查了各种资料说有不同的获取方法但是用到的最多的是[UIApplication sharedApplication].keyWindow和[[UIApplication sharedApplication].delegate window] 这两种方式。但是这两种获取window获取方式有什么区别呢,我特意从代码里面测试了一下。
一般情况下,我们在appdelegate文件中都会这样写



这说明在执行 didFinishLaunchingWithOptions: 这个代理方法时,在执行[self.window makeKeyAndVisible];方法之前,通过[UIApplication sharedApplication].keyWindow 方法获取不到window,
且在执行[UIApplication sharedApplication].keyWindow之前是要执行rootviewcontroller的代码的,因此先执行了controller中的代码,但是在controller中我们发现,在viewWillAppear和viewDidLoad 中的keywindow也是nil,但是在直到viewWillLayoutSubviews时才有了keywindow的值。但是无论何时都能获取到delegate.window。
也就是说除非在[self.window makeKeyAndVisible];后或者在之前的控制器里执行了viewWillLayoutSubviews后才会有值
记录在添加飘屏massary出错的问题
参考文章来源:https://blog.csdn.net/weixin_43883776/article/details/85017069