Last active
May 19, 2020 12:15
-
-
Save i03nomura1y/8017815 to your computer and use it in GitHub Desktop.
iOS LockScreen Notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did anybody solve this?