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");
}

你可能感兴趣的:(iOS-OC监听进入后台或者从后台启动)