Reputation: 580
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?
Upvotes: 10
Views: 3853
Reputation: 2426
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:
I.e. all times when the app appears on the screen after some action.
Upvotes: 0
Reputation: 3033
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:
Upvotes: 2