I'm building an alarm clock app and I want it to stay open during the night. I have a view that shows the time and instructs the user to keep the app open, and when the user starts the alarm, I set:
UIApplication.shared.isIdleTimerDisabled = true
That usually works, and the app stays active through the night - except, it doesn't ALWAYS work. Sometimes I'll find the app backgrounded in the morning. I've received similar reports from test users. The app doesn't seem to be crashing (I have crash reporting).
Does anyone have any ideas what could be happening, or even a good way to debug this? What event could cause the app to get backgrounded by the system (I can only think of an iOS auto update, but that's not the case)? What can I capture in applicationWillEnterBackground to figure this out?
Thanks!
UPDATE:
I've narrowed down the problem to activating the proximity sensor - I'm encouraging users to place the phone with the screen down, so the proximity sensor turns the screen off. After a lot of testing it's clear that the above issue only happens when the screen is down, never when it's up.
Another bit of information: I've added logging, and it seems that the app delegate's willResignActive() is getting called when the phone is picked up - so, for some reason the app gets backgrounded when the proximity sensor tries to turn the screen back on.
In light of this, any ideas? or ideas for debugging? I'm really stumped by this...
Thanks!