它在 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 在这里不能完美运行