Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 9 UIApplicationDidBecomeActiveNotification callback not called

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");}
like image 405
dorin Avatar asked Aug 03 '15 11:08

dorin


3 Answers

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.

like image 86
jypark Avatar answered Oct 14 '22 07:10

jypark


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];
});
like image 3
Jens Alen Avatar answered Oct 14 '22 06:10

Jens Alen


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 :/

like image 2
Benoît Avatar answered Oct 14 '22 07:10

Benoît