iOS-OC监听进入后台或者从后台启动

1、在AppDelegate中

//进入后台的时候调用
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    NSLog(@"进入后台");
}

//从后台进入的时候调用
- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    NSLog(@"回到app");
}

2、使用通知

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hadEnterBackGround) UIApplicationDidEnterBackgroundNotification object:nil];

- (void)hadEnterBackGround{
    NSLog(@"进入后台");
}

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

- (void)hadEnterForeGround{
    NSLog(@"回到app");
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容