I need to do something in applicationDidEnterBackground
. But I need to differentiate which user action causes the "enter background": screen lock or home button press.
I was using this code, which is from this post - How to differentiate between screen lock and home button press on iOS5?:
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateInactive) {
NSLog(@"Sent to background by locking screen");
} else if (state == UIApplicationStateBackground) {
NSLog(@"Sent to background by home button/switching to other app");
}
It works fine on iOS6. but on iOS7 (both device and simulator), I always get UIApplicationStateBackground
, whether the user clicks the home or the lock button.
Does someone have an idea about what could cause this? iOS 7 updates to multi-task background handling? Or some setting of my app (my app's background mode is off)?
And is there an alternative solution?
It doesn't work in Swift, you need to do some modification to make it work in Swift as follow
1.create a objectiveC file named LockNotifierCallback.m as follow:
create a head as well: #import
2.bridge this file to swift
3.add function to APPdelegate.swift:
PS:UIApplicationState doesn't work perfectly in here
This solution was taken from this answer and worked for me. It is a bit hacky but it makes it easier to be accepted on the appstore because it doesn't use com.apple.springboard.lockcomplete or com.apple.springboard.lockstate.
The following code goes into your AppDelegate:
The idea is to try changing the brightness of the screen after the application went to background and to check if this change was successful.
Disclaimer: it doesn't work on the simulator and you will have to test it on a real device.
This can help you both on iOS6 & iOS7 :).
When user press lock button you will get a
com.apple.springboard.lockcomplete
notification.