美文网首页iOS
ios 监听app从后台恢复到前台

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

作者: Melody_YM | 来源:发表于2016-12-02 10:09 被阅读2594次

APPDelegate中的代理方法可以监听到程序前后台切换的状态

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

- (void)applicationDidBecomeActive:(UIApplication * )application
{
    NSLog(@"---applicationDidBecomeActive----")
    // 进入前台
}

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

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSLog(@"---applicationDidBecomeActive----")
    // 进入前台
}

单个ViewController通过在ViewDidLoad里注册筛选器通知来监听,在dealloc中移除通知

//监听是否重新进入程序程序.
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(someMethod1:) 
name:UIApplicationDidBecomeActiveNotification object:nil]; 

//监听是否触发home键挂起程序.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification object:nil]; 

相关文章

网友评论

    本文标题:ios 监听app从后台恢复到前台

    本文链接:https://www.haomeiwen.com/subject/qkwfmttx.html