Skip to content

Instantly share code, notes, and snippets.

@junyixin
Last active October 13, 2023 10:12
Show Gist options
  • Save junyixin/68c8ced4723a949f9406b74c1633c23c to your computer and use it in GitHub Desktop.
Save junyixin/68c8ced4723a949f9406b74c1633c23c to your computer and use it in GitHub Desktop.
iOS锁屏通知(Objective-C)
//回调
static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
// "com.apple.springboard.lockcomplete" 通知总是接在 "com.apple.springboard.lockstate" 通知后面
CFStringRef nameCFString = (CFStringRef)name;
NSString *lockState = (__bridge NSString*)nameCFString;
NSLog(@"Darwin notification NAME = %@",name);
if([lockState isEqualToString:@"com.apple.springboard.lockcomplete"]) {
NSLog(@"锁屏");
} else {
NSLog(@"锁屏状态改变");
}
}
- (void)registerforDeviceLockNotif {
//锁屏通知
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
displayStatusChanged, // callback
CFSTR("com.apple.springboard.lockcomplete"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
displayStatusChanged, // callback
CFSTR("com.apple.springboard.lockstate"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment