0

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!

Yariv Adam
  • 844
  • 9
  • 24
  • use above code in viewWillDisappear. use setIdleTimer instead of using default setter. you can give a try. – Majid Bashir May 11 '18 at 06:24
  • Ok... can you explain what the rational is? Why in viewWillDisappear? won't that disable the idle timer when the user is in other views in my app? – Yariv Adam May 11 '18 at 07:15
  • Because before going to background, view has not disappeared yet, and view will disappear is something we might be able to hold that thing from going to background. – Majid Bashir May 11 '18 at 07:19
  • Setting isIdletimerDisabled in viewWillDisappear won't prevent the view from disappearing. It will just prevent the screen from dimming and eventually backgrounding the app after the user transitions to another view controller. – Yariv Adam May 11 '18 at 07:22

0 Answers0