Pages

Thursday, January 10, 2013

Re: Is there a way to check if the iOS device is in sleep mode?

I want to detect the lock/unlock events in the device, so far I got the darwin notifications as given below,:

 

-(void)registerForDarwinNotif

{

 

 

   

 

    //Screen lock notifications

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center

                                    NULL, // observer

                                    displayStatusChanged, // callback

                                    CFSTR("com.apple.iokit.hid.displayStatus"), // event name

                                    NULL, // object

                                    CFNotificationSuspensionBehaviorDeliverImmediately);

   

   

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center

                                    NULL, // observer

                                    displayStatusChanged, // callback

                                    CFSTR("com.apple.springboard.lockstate"), // event name

                                    NULL, // object

                                    CFNotificationSuspensionBehaviorDeliverImmediately);

   

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center

                                    NULL, // observer

                                    displayStatusChanged, // callback

                                    CFSTR("com.apple.springboard.hasBlankedScreen"), // event name

                                    NULL, // object

                                    CFNotificationSuspensionBehaviorDeliverImmediately);

   

 

   

   

}  

 

//My call back

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

{

       NSLog(@"IN Display status changed");

    // you might try inspecting the `userInfo` dictionary, to see

    //  if it contains any useful info

  

        NSLog(@"Darwin notification NAME = %@",name);

   

   

   

}

   

 

I am able to get the darwin notifications when device is locked/unlocked, but the real problem is how identify between if the notification has come from locking or the unlocking of the device. Console logs are:

 

Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] : IN Display status changed

Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] : Darwin notification NAME = com.apple.springboard.lockcomplete

Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] : IN Display status changed

Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] : Darwin notification NAME = com.apple.springboard.lockstate

Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] : IN Display status changed

Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] : Darwin notification NAME = com.apple.springboard.hasBlankedScreen

Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] : IN Display status changed

Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] : Darwin notification NAME = com.apple.iokit.hid.displayStatus


View the original article here

0 comments:

Post a Comment