在iOS7上区分屏幕锁定和主页按钮按下

Per*_*roy 12 cocoa-touch objective-c uiapplication ios ios7

我需要做点什么applicationDidEnterBackground.但我需要区分哪个用户操作导致"输入背景":屏幕锁定或按下主页按钮.

我正在使用这个代码,这是从这篇文章 - 如何区分iOS5上的屏幕锁定和主页按钮?:

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");
}
Run Code Online (Sandbox Code Playgroud)

它在iOS6上运行良好.但是在iOS7(设备和模拟器)上UIApplicationStateBackground,无论用户是单击主页还是锁定按钮,我总能得到.

有人知道可能导致这种情况的原因吗?iOS 7更新到多任务后台处理?或者我的应用程序的某些设置(我的应用程序的后台模式已关闭)?

还有替代解决方案吗?

wqq*_*wqq 18

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

当用户按下锁定按钮时,您将收到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];
Run Code Online (Sandbox Code Playgroud)

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您发现重复的问题,一旦您有15个代表,正确的操作是将帖子标记为重复.简单地在网站上发布相同的复制/粘贴答案不是我们所关心的,这会产生噪音. (2认同)