I need to do something in applicationDidEnterBackground
. But I need to differentiate which user action causes the \"enter background\": screen lock or home butt
It doesn't work in Swift, you need to do some modification to make it work in Swift as follow
1.create a objectiveC file named LockNotifierCallback.m as follow:
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
create a head as well: #import
@interface LockNotifierCallback : NSObject
+ (void(*)(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo))notifierProc;
@end
2.bridge this file to swift
3.add function to APPdelegate.swift:
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), nil, LockNotifierCallback.notifierProc(), "com.apple.springboard.lockcomplete", nil, CFNotificationSuspensionBehavior.DeliverImmediately)
PS:UIApplicationState doesn't work perfectly in here