How to implement an alarm with iPhone SDK 4.0 - iphone

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.

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.

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

Related

How to run a ~30sec process in the background every hour (iphone app)

I have an iphone app that has a 30second process that does some network IO. Basically, while the app is in the background, i want this process to run every hour (actually once a day, but if it fails i want it to re-run in an hours time).
With the background features of ios 4, is this possible? If so, how? What are the limitations that i'll come up against?
Thanks so much!
Take a look at Apple's documentation about running code in the background.
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html
There are few different ways of approaching backgrounded tasks. The only apps that can have fully backgrounded processes are "audio", "voip" and "location" apps, and this needs to be declared in the Info.plist.
If your app is not of this type, you'll probably find it difficult to do what you want easily. There are methods which allow you to keep your app alive in the background for a finite period of time (also at that link), but eventually your app will be shut down.
Local Notifications will only prompt the user to open the app - do you really want to have an alert pop-up on the phone every 30 seconds?
I was making some kind of similar research, have a look at this SO answer in case you didn't manage to find it before. Applications like DataMan or Data Usage must have some sort of periodic code execution in the background, so I'm not 100% convinced that what you're asking for is impossible..
I believe that Using Local notifications will help....
check following....
http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1
An application can create and schedule a local notification, and the operating system then delivers it at the schedule date and time. If it delivers it when the application is not active in the foreground, it displays an alert, badges the application icon, or plays a sound—whatever is specified in the UILocalNotification object. If the application is running in the foreground, there is no alert, badging, or sound; instead, the application:didReceiveLocalNotification: method is called if the delegate implements it.
The delegate can inspect the properties of the notification and, if the notification includes custom data in its userInfo dictionary, it can access that data and process it accordingly. On the other hand, if the local notification only badges the application icon, and the user in response launches the application, the application:didFinishLaunchingWithOptions: method is invoked, but no UILocalNotification object is included in the options dictionary.

How to show custom alert at specific time?

I'm working on an alarm/reminder application and want to show a custom alarm view when specified time is reached.
From what I understand, there are various (real and hypothetic) ways of doing it:
UILocalNotification. It would be perfect, but alert customization features are insufficient, e.g. I can't show a custom alert view, I can't play a sound for more than 30sec etc.
Keep application in the background and use NSTimer or other means to schedule execution after some time. Most likely won't work as the app may be killed any time.
Start my app at specified time to receive the notification. As far as I know it's not allowed at all.
After spending several days searching for a solution, I would give up and stick with UILocalNotification, but I DO know an app that shows custom alert view at specific time AND is available in AppStore. The app is named "Alarmed" and from what it looks like (last app view shown on alarm time before an actual alert; alarm still triggers even after the device is restarted) they found a way to start their app at specific time and still pass Apple's review.
Any ideas how this can be done?
Thanks.
It's not possible to show notifications other than what UILocalNotification offers you, unless the app is running in the foreground. (I just tried to install "Alarmed" and it uses a regular UILocalNotification like you'd expect).

UIApplication beginBackgroundTaskWithExpirationHandler question

guys :)
I have an iOS alarm app, which uses UILocalNotification-s to schedule the user defined alarms if the app enters background. Since this (in my implementation) can be a relatively long process, I need to make sure that the registering of the notifications is completed before the app goes inactive. I read that there is a method in UIApplication: beginBackgroundTaskWithExpirationHandler, which asks iOS for more time, so that it can complete its tasks, but I have no idea how to use it. These (void(^)(void)) parameters scare me :). I know it's too much to ask, but... if I have a method [self registerLocalNotifications], which registeres all local notifications, can you please point me how to make sure the method finishes before the app goes to background. Huge thanks!
This looks like good sample code where you can see the usage of blocks ^{} together with applicationDidEnterBackground: http://iphonesdkdev.blogspot.com/2010/04/local-push-notification-sample-code-os.html

iPhone session - timeout on inactivity

When the user successfully log into my app, session starts, after X period of inactivity the session will time out.
Now how do you detect, the user to see if his active using the application or not using it?
1) Check every on button?
2) check on screen load?
The most elegant way I've found to do this is to use a category on UIApplication that provides a sendEvent: method that resets an NSTimer (after calling its superclass implementation of course). If the timer goes off, post a notification. Listen for the notification everywhere you want to react to the timeout. Doing it this way means that you don't have to add code all through your app to reset the timer on every interaction.
Having said that, I agree with Shaggy Frog, this isn't typical behaviour for an iOS application, so it's not usually a good idea from a HCI perspective.
If you're asking "How do I define activity", you could always reset a timer after a touch event is received, whose callback triggers the "user is inactive event".
I can't speak much for your overall design choice here, as I don't think most iOS users expect their apps to behave in this manner. Think of your users and proceed with caution.
You can also check for applicationwillresignactive callback for this.

Is there a safe way to schedule an alert for an calendar app?

I want to make a special calendar app, but I am afraid it's not possible to safely schedule an alert for an event.
For example: I set up an alert for an event which starts in 3 months. I want to get notified 2 days before the event starts. In iOS 4 there is multitasking, so my app could run in the background all the time.
But now lets imagine it's a hardcore iPhone user who plays huge memory-intensive games all the time. At some point, iOS might kill my background app. Or the user might restart the device and forget to launch my app. So it could happen that the alert never happens. Bad thing.
Is there a safe way to ensure that an scheduled alert is thrown at the user, just like it is the case with the built in alarm clock app or the calendar app?
I'm going to bring back the EventKit notification - use event kit to schedule a calendar entry with an alert, and embed in there a URL that will open your app.
You could also use local notifications but this way the user will be able to see the upcoming event when reviewing the calendar, and even modify slightly if need be. They can't mess with a local notification once it's in place...
You want to use UILocalNotification for this.
EventKit will make it pop up in the user's calendar, maybe not what you want here.
BTW: Multitasking is really more "fast switching" than backgrounding in iOS... you won't be able to run arbitrary code in background, and you should expect to be killed anytime.