I recently submitted a new binary to the app store and sent it in for review and it was immediately rejected with the following message. "Unsupported operation - Apps are not allowed to listen to device lock notifications.". After some digging around I found out that we can't use "com.apple.springboard.lockstate" to figure out the lock state.
Essentially, my app needs to know how the user left my app. Whether it was pressing the home button, lock button, leaving the app through hitting another app's notification, etc. Is there any way of achieving this? I started investigating to see if my app were to run in the background, maybe we could check the application state to figure it out. That's as far as I got, I was wondering if anyone had anymore insight on this
2 Answers
Answers 1
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.
Answers 2
This cannot be done in a way that Apple will accept it in other words any notification key that’s not explicitly documented by Apple is considered private API.
And it´s true com.apple.springboard.lockstate
cannot be used anymore and is considered private.
You have a few ways to detect if the user has left the application:
applicationWillResignActive:
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
applicationWillEnterForeground:
Called when transitioning out of the background state
applicationDidEnterBackground:
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
None of these functions tells you how the user left the app except the applicationWillResignActive
which tells you that the app is about to go from active to inactive state for certain types of temporary interruptions.
0 comments:
Post a Comment