12

我需要在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");
}

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

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

是否有替代解决方案?

4

3 回答 3

19

这可以在 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];

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

于 2013-11-04T15:56:01.530 回答
2

该解决方案取自此答案并为我工作。它有点 hacky,但它更容易在 appstore 上被接受,因为它不使用com.apple.springboard.lockcompletecom.apple.springboard.lockstate

以下代码进入您的 AppDelegate:

func applicationDidEnterBackground(_ application: UIApplication) {
    if (DidUserPressLockButton()) {
        print("User pressed lock button")
    } else {
        print("user pressed home button")
    }
}

private func DidUserPressLockButton() -> Bool {
    let oldBrightness = UIScreen.main.brightness
    UIScreen.main.brightness = oldBrightness + (oldBrightness <= 0.01 ? (0.01) : (-0.01))
    return oldBrightness != UIScreen.main.brightness
}

这个想法是在应用程序进入后台后尝试更改屏幕的亮度并检查此更改是否成功。

免责声明:它在模拟器上不起作用,您必须在真实设备上进行测试。

于 2018-08-23T20:32:11.880 回答
0

它在 Swift 中不起作用,您需要进行一些修改以使其在 Swift 中起作用,如下所示

1.创建一个名为 LockNotifierCallback.m 的objectiveC文件,如下所示:

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

也创建一个头部:#import

@interface LockNotifierCallback : NSObject


+ (void(*)(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo))notifierProc;


@end

2.将此文件桥接到swift

3.在APPdelegate.swift中添加函数:

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

PS:UIApplicationState 在这里不能完美运行

于 2015-03-06T16:04:04.510 回答