Skip to content

Instantly share code, notes, and snippets.

@i03nomura1y
Last active May 19, 2020 12:15
Show Gist options
  • Save i03nomura1y/8017815 to your computer and use it in GitHub Desktop.
Save i03nomura1y/8017815 to your computer and use it in GitHub Desktop.
iOS LockScreen Notification
// iOS ロックスクリーン Notification
#define NotifName_LockComplete @"com.apple.springboard.lockcomplete"
#define NotifName_LockState @"com.apple.springboard.lockstate"
//call back
static void lockStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name_cf, const void *object, CFDictionaryRef userInfo){
NSString *name = (__bridge NSString*)name_cf;
if ([name isEqualToString:NotifName_LockComplete]) {
NSLog(@"DEVICE LOCKED");
} else if ([name isEqualToString:NotifName_LockState]) {
NSLog(@"LOCK STATUS CHANGED");
}
}
- (void)registerforDeviceLockNotif {
//Screen lock notifications
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
lockStatusChanged,
(__bridge CFStringRef)NotifName_LockComplete,
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
lockStatusChanged,
(__bridge CFStringRef)NotifName_LockState,
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
}
// LockScreen Notification
#define NotifName_LockComplete @"com.apple.springboard.lockcomplete"
static bool go_lockscreen_when_active = false; // アプリがアクティブ状態でロック画面に遷移したよフラグ
@implementation AppDelegate
// LockScreen Notification
static void didGoLockscreen(CFNotificationCenterRef center, void *observer, CFStringRef name_cf, const void *object, CFDictionaryRef userInfo){
NSString *name = (__bridge NSString*)name_cf;
if (![name isEqualToString:NotifName_LockComplete]) return;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
go_lockscreen_when_active = true;
}
}
- (void)registerforDeviceLockNotif {
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
didGoLockscreen,
(__bridge CFStringRef)NotifName_LockComplete,
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[self registerforDeviceLockNotif];
// ...
return YES;
}
- (void)applicationWillEnterForeground:(UIApplication *)application{
if (go_lockscreen_when_active) {
// アクティブ状態でロック画面になった == ロック画面から復帰
// ...
go_lockscreen_when_active = false;
} else {
// ホーム画面、別アプリ等から復帰
// ...
}
}
@end
@sauvikatinnofied
Copy link

Hi @i03nomura1y, I was trying to receive that screen lock/unlock notifications even when the application is not in foreground, i.e the application is running in background.For foreground mode, these notification is working fine, but I failed to received those notification while application is in background. Could you help me on that? How can I listen to those notifications even if the application is not running in foreground?

@EduardoVaca
Copy link

Hey I'm having the same problem. Have you solve it yet? :)

@tomasero
Copy link

Did anybody solve this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment