iOS-モニタロックイベント

2413 ワード

テキストアドレス
iPhoneのロックモニターは2つの方式に分けられています.
プログラムはフロントにありますが、これは簡単です.Darwinレイヤの通知を直接使用すればいいです.
#import 

#define NotificationLock CFSTR("com.apple.springboard.lockcomplete")

#define NotificationChange CFSTR("com.apple.springboard.lockstate")

#define NotificationPwdUI CFSTR("com.apple.springboard.hasBlankedScreen")
static void screenLockStateChanged(CFNotificationCenterRef center,void* observer,CFStringRef name,const void*object,CFDictionaryRef userInfo)

{

    NSString* lockstate = (__bridge NSString*)name;

    if ([lockstate isEqualToString:(__bridge  NSString*)NotificationLock]) {

        NSLog(@"locked.");

    } else {

        NSLog(@"lock state changed.");

    }

}
 static void screenLockStateChanged(CFNotificationCenterRef center,void* observer,CFStringRef name,const void* object,CFDictionaryRef userInfo)

{

    NSString* lockstate = (__bridge NSString*)name;

    if ([lockstate isEqualToString:(__bridge  NSString*)NotificationLock]) {

        NSLog(@"locked.");

    } else {

        NSLog(@"lock state changed.");

    }

}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    // Override point for customization after application launch.

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenLockStateChanged, NotificationLock, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenLockStateChanged, NotificationChange, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);

    //setScreenStateCb();

    return YES;

}

第二に、プログラムが後退した後、この時にスクリーンをロックすると上の通知が受け取れなくなり、もう一つの方法が必要で、ループでスクリーンをロックした状態かどうかを検出し、性能を消費し、アップルに掛けられる可能性がある.
 static void setScreenStateCb()

{
    uint64_t locked;
    __block int token = 0;
 notify_register_dispatch("com.apple.springboard.lockstate",&token,dispatch_get_main_queue(),^(int t){
    });
    notify_get_state(token, &locked);
    NSLog(@"%d",(int)locked);
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    while (YES) {
        setScreenStateCb();
        sleep(1);
    }
}