未调用iOS 9 UIApplicationDidBecomeActiveNotification回调

dor*_*rin 6 uiapplication nsnotificationcenter ios ios9

在iOS 9中,以下用于检测通知的代码不会触发选择器方法.在以前的版本(例如8.4)中它运行良好.有谁知道为什么?

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

- (void)yourMethod {NSLog(@"aaaaaaa");}
Run Code Online (Sandbox Code Playgroud)

小智 6

以下链接可能有助于解决您的问题.

OS X v10.11的基础发行说明

使用" addObserverForName "而不是"addObserver".

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

这将是工作.


Dan*_*ain -1

我就是这样进行通知注册的。

[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yourMethod:) name:UIApplicationDidBecomeActiveNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

像这样写方法。

- (void)yourMethod:(NSNotification *)notification
  {
     NSLog(@"aaaaaaa");
  }
Run Code Online (Sandbox Code Playgroud)

您还需要在 AppDelegate.m 中 NSLog 并在控制台中查看日志。

-(void)applicationDidBecomeActive:(UIApplication *)application{
      NSLog(@"applicationDidBecomeActive");
 }
Run Code Online (Sandbox Code Playgroud)