Differentiate between screen lock and home button press on iOS7

前端 未结 3 1850
生来不讨喜
生来不讨喜 2020-12-03 11:24

I need to do something in applicationDidEnterBackground. But I need to differentiate which user action causes the \"enter background\": screen lock or home butt

3条回答
  •  -上瘾入骨i
    2020-12-03 12:22

    This can help you both on iOS6 & iOS7 :).

    When user press lock button you will get a com.apple.springboard.lockcomplete notification.

    //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");
        }
    }
    

提交回复
热议问题