首页 > 技术文章 > 判断应用在前台还是在后台

hd1992 2016-01-14 16:15 原文

可以这样 判断

#import "AppDelegate.h"

 @interface AppDelegate () {

    BOOL isActive;

}

@end

@implementation AppDelegate

//进入后台

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

    isActive = NO;

}

//进入前台 

- (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.

}

 //处于活动状态

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

    isActive = YES;

}

@end

 

这样就可以根据isActive来控制一些通知的显示。

 

如:

- (void)application:(UIApplication *)application
    didReceiveLocalNotification:(UILocalNotification *)notification {

  if (isActive) {
    return;
  } else {
    HDFileTableViewController *vc = [[HDFileTableViewController alloc] init];
    vc.upLoad = NO;
    vc.directoryPath = [[NSSearchPathForDirectoriesInDomains(
        NSCachesDirectory, NSUserDomainMask, YES) lastObject]
        stringByAppendingPathComponent:@"附件"];
    UINavigationController *navc =
        [[UINavigationController alloc] initWithRootViewController:vc];

    [self.window.rootViewController presentViewController:navc
                                                 animated:YES
                                               completion:nil];
  }
}

 

推荐阅读