In iOS 9 the following code for detecting a notification does not fire the selector method. In previous versions (e.g. 8.4) it's running fine. Does anyone know why?
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(yourMethod)
name:UIApplicationDidBecomeActiveNotification
object:nil];
- (void)yourMethod {NSLog(@"aaaaaaa");}
This link as below maybe help your problem.
Foundation Release Notes for OS X v10.11
Use "addObserverForName" instead of "addObserver".
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification * Nonnull note) {
[self yourMethod];
}];
It will be work.
I hade the same problem and for me it worked to move the addObserver code to awakeFromNib. Another solution could be to add a delay to the addObserver as in the example below:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
});
While doing some tests, I noted that the notification is actually triggered, albeit not at launch time (or at least too early to be caught by your observer) (on the simulator only).
Try pulling the notification center down and up or the control center up and down, and you'll see your method will actually be called.
I'd suggest maybe calling your method manually on iOS 9, when your app starts?
On a real device, the method is called just as on iOS 8.
Edit: after further investigation, it seems the notification is actually not triggered every time on devices :/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With