应用程序状态枚举 UIApplicationState
:
1 typedef enum UIApplicationState : NSInteger { 2 UIApplicationStateActive, 3 UIApplicationStateInactive, 4 UIApplicationStateBackground 5 } UIApplicationState;
UIApplicationStateActive :应用程序运行在前台,目前接收事件。
UIApplicationStateInactive:应用程序运行在前台但不接收事件。这可能发生的由于一个中断或因为应用过渡到后台或者从后台过度到前台。
<1>电话进来或者其他中断事件
<2>从前台进入后台的过度事件
<3>从后台进入前台的过度事件
UIApplicationStateBackground :应用程序在后台运行
获取状态代码:
1、全局获取方式
1 UIApplicationState state = [UIApplication sharedApplication].applicationState; 2 if(state == UIApplicationStateActive){ 3 //code here... 4 }else if(state == UIApplicationStateBackground){ 5 //code here... 6 }else{ 7 //code here... 8 }
2、应用生命周期中获取
1 #AppDelegate.m 2 3 - (void)applicationWillResignActive:(UIApplication *)application { 4 UIApplicationState state = application.applicationState; 5 if(state == UIApplicationStateActive){ 6 //code here... 7 }else if(state == UIApplicationStateBackground){ 8 //code here... 9 }else{ 10 //code here... 11 } 12 }