IOS 9 UIApplicationDidBecomeActiveNotification callback not called

In iOS 9, the following code to detect a notification does not start the selection method. In previous versions (e.g. 8.4), it works fine. Does anyone know why?

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(yourMethod) name:UIApplicationDidBecomeActiveNotification object:nil]; - (void)yourMethod {NSLog(@"aaaaaaa");} 
+6
source share
6 answers

This link, as shown below, may help your problem.

Foundation for OS X v10.11 Release Notes

Use " addObserverForName " instead of "addObserver".

  [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * Nonnull note) { [self yourMethod]; }]; 

It will be a job.

+6
source

I had the same problem and for me it worked to move addObserver code to awakeFromNib. Another solution would be to add a delay to 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]; }); 
+3
source

While performing some tests, I noticed that the notification was indeed triggered, although not at launch time (or at least too soon to be caught by your observer) ( only on the simulator ).

Try pulling the notification center up and down or the control center up and down and you will see that your method will actually be called.

I suggest, maybe you will call your method manually on iOS 9, when will your application start?

On a real device, the method is called just like on iOS 8.

Edit : after further research, it seems that the notification does not actually start every time on devices: /

+2
source

I also ran into this problem, and adding an observer with a delay did not resolve it in my case. I managed to get rid of it by calling the method directly in AppDelegate and from the main queue without delay.

Swift:

 func applicationDidBecomeActive(application: UIApplication) { dispatch_async(dispatch_get_main_queue()) { () -> Void in yourMethod() } } 
0
source

I had the same problem only on real devices with iOS 9.0 and higher.

I ended up defining my own notification:
(this definition should be available worldwide, constants.h or similar)

 #define myAppBecameActiveNotif @"Tito, your app is active" 

Then, in the implementation of AppDelegate (in most cases, AppDelegate.m), you implement the applicationDidBecomeActive delegate method:

 - (void)applicationDidBecomeActive:(UIApplication *)application { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[NSNotificationCenter defaultCenter] postNotificationName:myAppBecameActiveNotif object:nil]; }); } 

(Just a delay in sending your notification a bit so that your components come to life)

Now, in the class you would like to be notified about, the application has become active, you do

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(whatToDoWhenAppActive:) name:myAppBecameActiveNotif object:nil]; 

Now the work is good. You will see that you cannot feel this delay of 500 ms ...

0
source

I am notifying you so.

 [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yourMethod:) name:UIApplicationDidBecomeActiveNotification object:nil]; 

Write a method like this.

 - (void)yourMethod:(NSNotification *)notification { NSLog(@"aaaaaaa"); } 

Also you need NSLog in AppDelegate.m and see the login console.

 -(void)applicationDidBecomeActive:(UIApplication *)application{ NSLog(@"applicationDidBecomeActive"); } 
-1
source

All Articles