I have found a better way to do this thing.
Differentiate between screen lock and home button press on iOS7
This can help you both on iOS5 & iOS6 & iOS7 device :).But it don't work at iOS Simulator.
You can get [[UIApplication sharedApplication] applicationState]
to judge whether it is lock screen or home button with UIApplicationStateInactive
and UIApplicationStateBackground
before iOS7.
But these became the same state UIApplicationStateBackground
on iOS7, and you can judge it through screen brightness.
The screen brightness should be 0 when in lock screen state; otherwise, it is home button state.
You can put the code snippet in - (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateInactive) {
NSLog(@"Sent to background by locking screen");
} else if (state == UIApplicationStateBackground) {
CGFloat screenBrightness = [[UIScreen mainScreen] brightness];
NSLog(@"Screen brightness: %f", screenBrightness);
if (screenBrightness > 0.0) {
NSLog(@"Sent to background by home button/switching to other app");
} else {
NSLog(@"Sent to background by locking screen");
}
}
}