系统消息通知

  • UIApplicationDidBecomeActiveNotification ------ 后台进前台通知
  • UIApplicationWillTerminateNotification ------- 应用在前台,双击Home键,杀掉调用 ,但是应用在后台,双击Home键, 再终止应用不会被通知
  • UIApplicationWillResignActiveNotification ------- Home键按下通知
  • UIApplicationDidEnterBackgroundNotification ----- 进入后台通知
  • UIApplicationDidFinishLaunchingNotification ------ 第一次点击icon启动项目的时候通知
  • UIApplicationDidReceiveMemoryWarningNotification ---- 内存警告的时候通知
  • UIApplicationSignificantTimeChangeNotification ----- 无论是改变时区还是改变时间 然后重回app的时候通知
  • NSSystemClockDidChangeNotification ---- 如果只改变时区不改变时间的话不调用,只要改时间才通知
  • UIApplicationWillChangeStatusBarOrientationNotification ----- 将要改变状态栏方向通知
  • UIApplicationStatusBarOrientationUserInfoKey ---- 获取当前设备的旋转方向
  • UIApplicationDidChangeStatusBarOrientationNotification ---- 改变状态栏方向通知
  • UIApplicationWillChangeStatusBarFrameNotification ----- 将要改变状态栏frame通知
  • UIApplicationDidChangeStatusBarFrameNotification ---- 改变状态栏frame通知
  • UIApplicationStatusBarFrameUserInfoKey ---- 获取当前设备的状态frame
  • UIApplicationBackgroundRefreshStatusDidChangeNotification ----- 在后台下载内容的应用程序的状态变化时候通知
  • UIApplicationProtectedDataWillBecomeUnavailable — 锁屏之后 解锁前的通知
  • UIApplicationProtectedDataDidBecomeAvailable ------ 在做iOS监听开屏时通知

具体代码

//后台进前台通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
    
//应用在前台,双击Home键,杀掉调用 ,但是应用在后台,双击Home键, 再终止应用不会被通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willTerminate) name:UIApplicationWillTerminateNotification object:nil];

//Home键按下通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willResignActive) name:UIApplicationWillResignActiveNotification object:nil];

//进入后台通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];

//第一次点击icon启动项目的时候通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishLaunching) name:UIApplicationDidFinishLaunchingNotification object:nil];

//内存警告的时候通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];

//无论是改变时区还是改变时间 然后重回app的时候通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(significantTimeChange) name:UIApplicationSignificantTimeChangeNotification object:nil];

//如果只改变时区不改变时间的话不调用,只要改时间才通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clockDidChange) name:NSSystemClockDidChangeNotification object:nil];

//将要改变状态栏方向通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willChangeStatusBarOrientation:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
//UIApplicationStatusBarOrientationUserInfoKey 获取当前设备的旋转方向

//改变状态栏方向通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeStatusBarOrientation) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

//将要改变状态栏frame通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willChangeStatusBarFrame) name:UIApplicationWillChangeStatusBarFrameNotification object:nil];

//改变状态栏frame通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeStatusBarFrame:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
//UIApplicationStatusBarFrameUserInfoKey 获取当前设备的状态frame

//在后台下载内容的应用程序的状态变化时候通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backgroundRefreshStatusDidChange) name:UIApplicationBackgroundRefreshStatusDidChangeNotification object:nil];

//锁屏之后 解锁前的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(protectedDataWillBecomeUnavailable) name:UIApplicationProtectedDataWillBecomeUnavailable object:nil];

//在做iOS监听开屏时通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(protectedDataDidBecomeAvailable) name:UIApplicationProtectedDataDidBecomeAvailable object:nil];

实现方法

//应用在前台,双击Home键,杀掉调用 ,但是应用在后台,双击Home键, 再终止应用不会被调用.
- (void)willTerminate {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

//进入后台执行方法
- (void)didEnterBackground {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

//第一次点击icon启动项目的时候调用
- (void)didFinishLaunching {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

//Home键按下执行方法
- (void)willResignActive {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

//内存警告的时候 调用
- (void)didReceiveMemoryWarning {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

//无论是改变时区还是改变时间 然后重回app的时候通知
- (void)significantTimeChange {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

//将要改变状态栏方向通知
- (void)willChangeStatusBarOrientation:(NSNotification *)noti {
    //UIApplicationStatusBarOrientationUserInfoKey 获取当前设备的旋转方向
    NSLog(@"%@", NSStringFromSelector(_cmd));
    UIInterfaceOrientation newOrientation = [[noti.userInfo valueForKey:UIApplicationStatusBarOrientationUserInfoKey] integerValue];
    NSLog(@"new orientation: %ld", (long)newOrientation);
}

//改变状态栏方向通知
- (void)didChangeStatusBarOrientation {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

//将要改变状态栏frame通知
- (void)willChangeStatusBarFrame{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

//改变状态栏frame通知
- (void)didChangeStatusBarFrame:(NSNotification *)noti  {
    NSLog(@"%@", NSStringFromSelector(_cmd));
    //UIApplicationStatusBarFrameUserInfoKey 获取当前设备的状态frame
    NSValue *rectValue = [noti.userInfo objectForKey:UIApplicationStatusBarFrameUserInfoKey];
    CGRect statusRect = [rectValue CGRectValue];
    NSLog(@"%@", NSStringFromCGRect(statusRect));
}


//如果只改变时区不改变时间的话不调用,只要改时间才通知
- (void)clockDidChange {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

//在后台下载内容的应用程序的状态变化时候调用
- (void)backgroundRefreshStatusDidChange {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

//锁屏之后 解锁前的调用
- (void)protectedDataWillBecomeUnavailable {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

//在做iOS监听开屏时 调用
- (void)protectedDataDidBecomeAvailable {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

//每次后台进前台都会执行这个方法
- (void)didBecomeActive {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

移除通知

- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self]; //移除通知
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐