在iOS7上区分屏幕locking和主页button

我需要在applicationDidEnterBackground做一些事情。 但我需要区分哪些用户行为导致“进入背景”:屏幕locking或主页按下button。

我正在使用这个代码,这是从这篇文章 – 如何区分iOS5上的屏幕locking和主页button? :

 UIApplicationState state = [application applicationState]; if (state == UIApplicationStateInactive) { NSLog(@"Sent to background by locking screen"); } else if (state == UIApplicationStateBackground) { NSLog(@"Sent to background by home button/switching to other app"); } 

它在iOS6上正常工作。 但在iOS7(设备和模拟器),我总是得到UIApplicationStateBackground ,无论用户点击主页还是lockingbutton。

有人有什么可能导致这个问题的想法吗? iOS 7更新到多任务后台处理? 或者我的应用程序的一些设置(我的应用程序的背景模式closures)?

还有其他解决scheme吗?

这可以帮助你在iOS6和iOS7 :)。

当用户按下lockingbutton,你会得到一个com.apple.springboard.lockcomplete通知。

 //new way //put this in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, displayStatusChanged, CFSTR("com.apple.springboard.lockcomplete"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately); //put this function in AppDelegate static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { if (name == CFSTR("com.apple.springboard.lockcomplete")) { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"kDisplayStatusLocked"]; [[NSUserDefaults standardUserDefaults] synchronize]; } } //put this in onAppEnterBackground UIApplicationState state = [[UIApplication sharedApplication] applicationState]; if (state == UIApplicationStateInactive) { NSLog(@"Sent to background by locking screen"); } else if (state == UIApplicationStateBackground) { if (![[NSUserDefaults standardUserDefaults] boolForKey:@"kDisplayStatusLocked"]) { NSLog(@"Sent to background by home button/switching to other app"); } else { NSLog(@"Sent to background by locking screen"); } } //put this in - (void)applicationWillEnterForeground:(UIApplication *)application [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"kDisplayStatusLocked"]; [[NSUserDefaults standardUserDefaults] synchronize]; 

 CGFloat screenBrightness = [[UIScreen mainScreen] brightness]; NSLog(@"Screen brightness: %f", screenBrightness); UIApplicationState state = [[UIApplication sharedApplication] applicationState]; if (state == UIApplicationStateInactive) { NSLog(@"Sent to background by locking screen"); } else if (state == UIApplicationStateBackground) { if (screenBrightness > 0.0) { NSLog(@"Sent to background by home button/switching to other app"); } else { NSLog(@"Sent to background by locking screen"); } } 

它不能在Swift中工作,你需要做一些修改,使其在Swift中工作如下

1.创build一个名为LockNotifierCallback.m的目标C文件,如下所示:

 static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { if ([(__bridge NSString *)name isEqual: @"com.apple.springboard.lockcomplete"]) { NSLog(@"Screen Locked"); [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"kDisplayStatusLocked"]; [[NSUserDefaults standardUserDefaults] synchronize]; } } @implementation LockNotifierCallback + (void(*)(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo))notifierProc { return displayStatusChanged; } @end 

创build一个头:#import

 @interface LockNotifierCallback : NSObject + (void(*)(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo))notifierProc; @end 

2.bridge这个文件很快

3.添加到APPdelegate.swift的function:

 CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), nil, LockNotifierCallback.notifierProc(), "com.apple.springboard.lockcomplete", nil, CFNotificationSuspensionBehavior.DeliverImmediately) 

PS:UIApplicationState在这里不起作用