ios 监听app从后台恢复到前台

正常情况下,在AppDelegate中实现下面两个方法,能够监听从后台恢复到前台

[cpp] 
- (void)applicationDidEnterBackground:(UIApplication *)application 

    log4info(@"---applicationDidEnterBackground----"); 
   //进入后台  

 
- (void)applicationDidBecomeActive:(UIApplication *)application 

 
    NSLog(@"---applicationDidBecomeActive----"); 
    //进入前台  
 
     
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    log4info(@"---applicationDidEnterBackground----");
   //进入后台
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{

NSLog(@"---applicationDidBecomeActive----");
    //进入前台

}

但是单个UIViewController怎么监听呢

在-viewDidLoad方法中,添加一下代码监听notification

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

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(someMethod:)
                                             name:UIApplicationDidBecomeActiveNotification object:nil];
别忘了在-dealloc中将监听移除:

[cpp] 
[[NSNotificationCenter defaultCenter] removeObserver:self];

[[NSNotificationCenter defaultCenter] removeObserver:self];

上一篇:SSM框架整合开发


下一篇:利用dropbox备份vps数据