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
@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