views:

658

answers:

2

I have an idea for a pretty unusual alarm clock fir the iPhone. But as of now I have some thoughts on how to actually implement this. First off: forgetting about background services for now, how would I do the actual timer that fires the alarm etc? A separate thread? Or does the SDK include any nice alarm features I missed? Of cause I need to be as battery efficient as possible. But for now I do no background process.

Please advise me on this as it is a crucial concept of this app, if it will work or not.

+2  A: 

I would create a local notification. Compared to NSTimer, local notifications have the advantage that they work regardless of whether your app is running or not.

Ole Begemann
Seems interesting, however. I also need to be able to do some GPS stuff and downloading/parsing of an XML. Is this possible to do in background?
sebrock
I guess not. I'm leaning towards NSTimer now anyway...
sebrock
To answer your Q:"Is this possible to do in background" -- Yes, it is possible...but you have to declare your app as requiring location (It does this by including the UIBackgroundModes key in its Info.plist file and setting the value of this key to an array containing the "location" string) ... just be aware it is going to keep the GPS system enabled and thus be battery-draining.
Jann
OK, these restrictions makes my idea somewhat complicted so I think I'll have to go with NSTimer and running in the foreground anyway.
sebrock
+1  A: 

You must create a UILocalNotification object and schedule it with scheduleLocalNotification, it is quite simple but there are strong limitations. You app may or may not be notified that the alarm occurred. When the alarm is presented, it will be in form of a AlertView. If the user taps "Close" you do not get a notification. If he taps "View Details", then you get the event didReceiveLocalNotification, your app moves to the foreground and can do whatever you want.

You can also register with iOS4 to receive updates on location change. The parsing of XML can be done but again there are limitations as to how much time you have to run methods in the background.

All information along with sample code can be found here: http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html

Rafael
Yes, this will not work as I need to check the location only minutes before the actual alarm goes off. The time span between setting the alarm and fire could be hours. I think I'll just have to do the standard NSTimer method with the app in the forground.
sebrock