0

Does anybody know if there is an xcode unlock event that can track if somebody unlocks his iPhone?

Or is this only possible with jailbroken devices?

Thanks in advance.

GoGauchos
  • 588
  • 5
  • 11
Teddy
  • 23
  • 1
  • 4

1 Answers1

1

Yes you can do this on a non-jailbroken device using CFNotificationCenterAddObserver.

Add an observer for the Darwin notification 'lockstate':

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), 
                            NULL, 
                            lockStateDidNotify, 
                            CFSTR("com.apple.springboard.lockstate"),
                            NULL, 
                            CFNotificationSuspensionBehaviorDeliverImmediately);

and you will be notified each time the device locks/unlocks:

static void lockStateDidNotify(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) 
{
    NSLog(@"The Device Locked/Unlocked");
}

Since this fires for both a lock and an unlock it is not entirely what you want, but you can observe com.apple.springboard.lockcomplete (which only fires during a lock) and check to see if you get both. If you don't get the lockcomplete you can assume an unlock just happened.

davbryn
  • 7,156
  • 2
  • 24
  • 47
  • These darwin notifications are private API and hence apple rejects apps using them. Is there any public APIs to do this? – Geek Jan 24 '14 at 05:36