10

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?

jimmyg
  • 580
  • 4
  • 13
  • 1
    possible duplicate of [Lock Unlock events iphone](http://stackoverflow.com/questions/706344/lock-unlock-events-iphone) – Cody Gray - on strike Jan 08 '12 at 03:51
  • Probably the reason, you didn't receive the said notifications is because of the following: "These are only enforced when a user has their device Passcode Protected, Data Protection is not available in the Simulator." – Vibhor Goyal Dec 20 '13 at 19:08

2 Answers2

2

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

Community
  • 1
  • 1
Ryan Crews
  • 3,015
  • 1
  • 32
  • 28
  • Thanks! That's a clever technique. However, the app would have to stay alive in the background, which I would rather avoid. – jimmyg Jan 08 '12 at 04:27
  • True, ideally a notification similar to UIApplicationProtectedDataWillBecomeUnavailable would be sent. – Ryan Crews Jan 08 '12 at 06:08
0

There is more simple way - just catch UIApplicationDidBecomeActiveNotification notification and measure time passed since last catch. But it's requires to reset the timer when user taps the app. It can be done catching all tap events to the app window. Time between last tap and UIApplicationDidBecomeActiveNotification will give you real timeout. Also need to check time between current and recent UIApplicationDidBecomeActiveNotification - it may be less than between last tap and UIApplicationDidBecomeActiveNotification.

UIApplicationDidBecomeActiveNotification fired when:

  1. App launches
  2. App restored from background
  3. App being unlocked
  4. Phone call ended

I.e. all times when the app appears on the screen after some action.

AlexeyVMP
  • 2,386
  • 3
  • 24
  • 31
  • Thanks. That would be helpful if only it told me whether the device was locked since the last time the app ran. The app needs to prompt for a password if the the device was locked. If it was not locked, it needs to see if it has been 60 minutes since it last prompted for a password. I can handle checking for 60 minutes. I just don't know if the phone was previously locked. – jimmyg Jan 09 '12 at 20:35