Lock Unlock events iphone Lock Unlock events iphone ios ios

Lock Unlock events iphone


You can use Darwin notifications, to listen for the events. From my testing on a jailbroken iOS 5.0.1 iPhone 4, I think that one of these events might be what you need:

com.apple.springboard.lockstatecom.apple.springboard.lockcomplete

Note: according to the poster's comments to a similar question I answered here, this should work on a non-jailbroken phone, too.

To use this, register for the event like this (this registers for just the first event above, but you can add an observer for lockcomplete, too):

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center                                (void*)self, // observer (can be NULL)                                lockStateChanged, // callback                                CFSTR("com.apple.springboard.lockstate"), // event name                                NULL, // object                                CFNotificationSuspensionBehaviorDeliverImmediately);

where lockStateChanged is your event callback:

static void lockStateChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {    NSLog(@"event received!");    if (observer != NULL) {        MyClass *this = (MyClass*)observer;    }    // you might try inspecting the `userInfo` dictionary, to see     //  if it contains any useful info    if (userInfo != nil) {        CFShow(userInfo);    }}

The lockstate event occurs when the device is locked and unlocked, but the lockcomplete event is only triggered when the device locks. Another way to determine whether the event is for a lock or unlock event is to use notify_get_state(). You'll get a different value for lock vs. unlock, as described here.


Round about answer:

Application will resign active gets called in all sorts of scenarios... and from all my testing, even if your application stays awake while backgrounded, there are no ways to determine that the screen is locked (CPU speed doesn't report, BUS speed remains the same, mach_time denom / numer doesn't change)...

However, it seems Apple does turn off the accelerometer when the device is locked... Enable iPhone accelerometer while screen is locked(tested iOS4.2 on iPhone 4 has this behavior)

Thus...

In your application delegate:

- (void)applicationWillResignActive:(UIApplication *)application{    NSLog(@"STATUS - Application will Resign Active");    // Start checking the accelerometer (while we are in the background)    [[UIAccelerometer sharedAccelerometer] setDelegate:self];    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1]; // Ping every second    _notActiveTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(deviceDidLock) userInfo:nil repeats:NO]; // 2 seconds for wiggle}//Deprecated in iOS5- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{    NSLog(@"STATUS - Update from accelerometer");    [_notActiveTimer invalidate];    _notActiveTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(deviceDidLock) userInfo:nil repeats:NO];}- (void)deviceDidLock{    NSLog(@"STATUS - Device locked!");    [[UIAccelerometer sharedAccelerometer] setDelegate:nil];    _notActiveTimer = nil;}- (void)applicationDidBecomeActive:(UIApplication *)application{    NSLog(@"STATUS - Application did become active");    [[UIAccelerometer sharedAccelerometer] setDelegate:nil];    [_notActiveTimer invalidate];    _notActiveTimer = nil;}

I know... It's kind of a hack, but it has worked like a charm for me so far. Please update if you see any issues that prevent this from working.


There is a prettier way of telling apart task switching and screen locking-originated applicationWillResignActive: callbacks which doesn't even involve undocumented features such as the accelerometer state.

When the app is moving to the background, the app delegate is first sent an applicationWillResignActive:, then an applicationDidEnterBackground:. When the app is interrupted by pressing the Lock button or by an incoming phone call, the latter method is not called. We can use this information to distinguish between the two scenarios.

Say you want to be called back in the screenLockActivated method if the screen gets locked. Here's the magic:

- (void)applicationWillResignActive:(UIApplication*)aApplication{    [self performSelector:@selector(screenLockActivated)               withObject:nil               afterDelay:0];}- (void)applicationDidEnterBackground:(UIApplication*)aApplication{    [NSObject cancelPreviousPerformRequestsWithTarget:self];}- (void)screenLockActivated{    NSLog(@"yaay");}

Explanation:

By default, we assume that every call to applicationWillResignActive: is because of an active->inactive state transition (as when locking the screen) but we generously let the system prove the contrary within a timeout (in this case, a single runloop cycle) by delaying the call to screenLockActivated. In case the screen gets locked, the system finishes the current runloop cycle without touching any other delegate methods. If, however, this is an active->background state transition, it also invokes applicationDidEnterBackground: before the end of the cycle, which allows us to simply cancel the previously scheduled request from there, thus preventing it from being called when it's not supposed to.

Enjoy!