How to differentiate lock screen and home button (background multitasking) on applicationWillResignActive in the app delegate
Have you tried implementing -applicationDidEnterBackground:
in your app delegate?
Can we detect whether a user left through the home button or lock button without listening to darwin notifications?
After searching through the documentation from apple and digging through a ton of threads I think I might have stumbled upon the solution.
As far as I know this is currently the only way of detecting whether a user left through the home button or lock button (I don't believe this works on the simulator you have to try it on an actual phone).
Inside of this delegate (And it will only work when called in this delegate)
func applicationDidEnterBackground(_ application: UIApplication) {
}
You can call this little snippet here:
func DidUserPressLockButton() -> Bool {
let oldBrightness = UIScreen.main.brightness
UIScreen.main.brightness = oldBrightness + (oldBrightness <= 0.01 ? (0.01) : (-0.01))
return oldBrightness != UIScreen.main.brightness
}
Usage:
func applicationDidEnterBackground(_ application: UIApplication) {
if (DidUserPressLockButton()) {
//User pressed lock button
} else {
//user pressed home button
}
}
EXPLANATION:
It seems that apple only lets you change the screen brightness from applicationDidEnterBackground
when the user left through the lock button and not the home button. So the idea is to change the screen brightness with a miniscule amount and check to see if it was able to be changed. This seems kinda hacky but I've heard that this is actually working as intended. As far as testing it goes it seems to be working 100% of the time. I could not find any issues with this except for users that really do want to change the brightness of the screen. I hope someone else can find something less hacky and more concrete.
Detect screen lock or home button press on iOS9 and iOS10 in Swift 3
This may work for you.
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
nil,
{ (_, observer, name, _, _) in
print("received notification: \(name)")
},
"com.apple.springboard.lockcomplete" as CFString!,
nil,
.deliverImmediately)
You can check this answer for more detail.
Is it possible to distinguish between locking the device and sending an app to background?
iOS 6
In my preliminary testing via the simulator, checking the application state with
[[UIApplication sharedApplication] applicationState]
in either
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
allows you to differentiate between a call to lock the device and just switching back to the homescreen. A lock screen will return 1 (UIApplicationStateInactive), whereas a home button press will register as a 2 (UIApplicationStateBackground).
It seems consistent and should work on an iOS device just as reliably as it does in the simulator.
iOS 7
The iOS 6 method no longer works in iOS 7. In order to do this now, you have to utilize CFNotificationCenter and listen for a darwin notification (labeled: com.apple.springboard.lockcomplete). You can find the github repo with the sample project here: https://github.com/binarydev/ios-home-vs-lock-button
Credit for the iOS 7 fix goes out to wqq
Related Topics
Swift 3 - How to Make Timer Work in Background
Instagram Hooks Pre-Select Media Issue
Go VS. Return Button in iOS Keyboard for HTML Input Forms
How to Convert Fbprofilepictureview to an Uiimage
How to Set Http Header Fields in Objective-C
Determine User's "Temperature Unit" Setting on iOS 10 (Celsius/Fahrenheit)
How to Only Disable Scroll in Scrollview But Not Content View
Uilabel with Two Different Color Text
Applicationwillterminate: Not Being Called
Swift - Fatal Error: Array Index Out of Range
Open Links in Safari Instead of Uiwebview
Custom Repeat Interval for Uilocalnotification
Open Installed Pwa from External Url
Nsoperationqueue Serial Fifo Queue
Exception Type: Exc_Crash (Sigabrt)
Error in Xcode 6 - View Controller Does Not Have an Outlet Named (Subview)
Unarchive Array with Nskeyedunarchiver Unarchivedobject(Ofclass:From:)