1

currently I'm developing a iPhone app that automatically turns on the light in my room. Therefore my app needs an alarm clock function, which means I have to find a way to trigger an event at a specific time.

In the moment I create UILocalNotification that fires when the alarm time has been reached, so I can handle the event in the didReceiveLocalNotification-method. My problem is that this method is not called when the phone is locked. In this case UILocalNotification only appears on the screen and you have to unlock the device to trigger the event.

There has to be a way to run code even when your phone is locked. I know some alarm clock apps have already found a solution for this problem. The alarm clock app SleepCycle plays a sound even if the app is in background.

So if anyone knows a trick or workaround to solve my problem, I would be very happy about responses.

André
  • 464
  • 4
  • 17
  • Use [Background Execution](https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html) not local notification. See the section **Communicating with an External Accessory** – Brain89 Jan 28 '15 at 13:08
  • Ok, thanks for your suggestion. If I understand things correctly, I have only 10 mins to complete my background task with beginBackgroundTaskWithExpirationHandler. Do you know, how I could schedule a task that is 8h in the future? – André Jan 28 '15 at 13:37
  • You have only 10 **second** to complete your task. Could you describe how do you communicate with the system of a room light? – Brain89 Jan 28 '15 at 13:47
  • I could turn on the light (or any other device) by sending a http-GET-request to an Arduino with ethernet shield. The Arduino controls a wireless socket on 433MHz. My plan is to implement a "wake up light" function in my remote control app, that sends the ON-command at a specific time. – André Jan 28 '15 at 14:07
  • 1
    it's possible to perform this task when the App receives a silent push. But this requires a server which is sending the push at specific time and a stable Internet connection. Also you could check [this article](http://oleb.net/blog/2014/02/alarm-clock-apps-ios/) that explains how iOS alarms work. Or you could add background fetching in your app and check in the handler if wake up time is your alarm time. – Brain89 Jan 28 '15 at 14:33
  • Ok, I think the push notification is not an option for me. So I will try to use the background fetching, but I already suspect that it will not work very accurately, because the OS decides whenever to call the fetch method. – André Jan 28 '15 at 15:01
  • The "Opt out of Multitasking" solution sounds quite promising. Thank you for the suggestions. I'll experiment a bit. – André Jan 28 '15 at 15:07
  • Did you find a solution? Seems like External Accessory framework is only for MFi certified devices. Doesn't the Arduino have its own clock, so your app can send a command to schedule the light rather than rely on the iOS device to handling the scheduling? – jrc Oct 06 '15 at 22:16
  • Hey jrc, yeah finally this was they way I solved the problem. I used a "RTC DS3231 AT24C32" Chip for the Arduino, which a has an alarm function. It's a much better idea to use a external server to control timed commands, than to rely on a iOS device. – André Oct 09 '15 at 10:33

0 Answers0