Short answer: No.
Long answer: nothing at UIKit guarantees KVO compliance. If you find that KVO-ing property is working, be grateful, this is unintentional. Also: be careful. In the future, this could be torn.
If you find that this is what you need, write a request for improvement .
About your real code, it is inherently flawed. DO NOT try to add the "rootViewController" installer to the UIWindow this way. It will crash when you compile your code on iOS 4, but someone runs it on an iOS 5 device. Since you compiled using the SDK 4.x, the #if statements will evaluate to true, which means your category method smashher will be included in the binary. However, when you run it on your iOS 5 device, you will now get a method conflict because the two methods on UIWindow will have the same method signature, and there is no guarantee which one will be used.
Do not twist with such frames. If you need to do this, use a subclass. THIS WHY EXISTS THIS.
Your subclass will look something like this:
@interface CustomWindow : UIWindow @property (nonatomic, retain) UIViewController *rootViewController; @end @implementation CustomWindow : UIWindow static BOOL UIWindowHasRootViewController = NO; @dynamic rootViewController; - (void)_findRootViewControllerMethod { static dispatch_once_t predicate; dispatch_once(&predicate, ^{ IMP uiwindowMethod = [UIWindow instanceMethodForSelector:@selector(setRootViewController:)]; IMP customWindowMethod = [CustomWindow instanceMethodForSelector:@selector(setRootViewController:)]; UIWindowHasRootViewController = (uiwindowMethod != NULL && uiwindowMethod != customWindowMethod); }); } - (UIViewController *)rootViewController { [self _findRootViewControllerMethod]; if (UIWindowHasRootViewController) {
Caveat Implementor: I typed this in a browser window
Dave DeLong Jul 07 2018-11-11T00: 00Z
source share