Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you tell if the iphone gets locked?

Tags:

ios

My app has requirement to prompt for a password if it has been more than 60 minutes since they last entered their password, or if the user enters the app after having locked the device.

The problem is knowing when the device gets locked. If the user is just switching between applications, the app does not need to prompt for a password unless it has been 60 minutes since the last password prompt. If they lock the device, prompt again even if its been less than 60 minutes.

Notifications such applicationWillResignActive do not help because I cannot determine if the app is resigning active due to the device being locked or the user switching to another app.

In searching, I found posts that say I can register to observe the UIApplicationProtectedDataWillBecomeUnavailable notification.

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(protectedDataWillBecomeUnavailable:)
           name:UIApplicationProtectedDataWillBecomeUnavailable object:nil];

When I get this notification I set a flag to prompt for the user's password the next time the app becomes active. But my testing has shown that this notification is not sent unless the device has a passcode setup.

I have written test code to observe all notifications, and I do not see any other notifications that would indicate that the device is getting locked.

Is there any other way to know when the device gets locked?

like image 956
jimmyg Avatar asked Jan 08 '12 03:01

jimmyg


1 Answers

This question intrigued me, so I looked around a little out of curiosity. While it doesn't appear there's a handy notification sent, there does appear to be a clever hack using the accelerometer. Here's a link to that:

Lock Unlock events iphone

like image 141
Ryan Crews Avatar answered Oct 07 '22 05:10

Ryan Crews