UILocalNotification handling when screen is locked - iphone

I am developing an iPhone app that delivers alerts at certain times using UILocalNotifications. Pressing the OK button on the alert launches the app so it can perform specific tasks.
So far, everything works beautifully in most cases: if the app is running and the alert fires, it works fine, and if the app is not running (but the phone is on), it works just as well.
However, if the alert fires when the screen is locked, one of two things happens.
alert fires, and I "slide to unlock" immediately, then the app launches as expected.
alert fires, but I wait to "slide to unlock" longer than 20 seconds (the time it takes the screen to lock (dim) again). When I finally unlock the screen, the alert shows, but does NOT launch the app.
I have looked throughout the Apple docs and this site, but cannot find an answer. I hope my explanation makes sense. Any thoughts?

I think that behaviour makes sense and shouldn't be circumvented (which I think is not possible). If the user slides to unlock just after a few seconds after the notification, the propability is high, that he slided to unlock just because of that notification and wants to get into the app. If a lot of time is passed, the notification is still shown, but the propability is low, that the user slides to unlock because of this notification. It is more likely that he just wants to write an email or do something completely different. So the app of the last notification shouldn't get started.

Thanks for your quick and clear answer, Dominik. Quick follow-up: the purpose in relaunching the app is to schedule another alert. If I send several alerts at once (scheduled for different times), and the user does not unlock the phone for any of them, do they all appear at the same time the first time the user unlocks? (I would only want the last one to appear)
Thanks again.

Related

controlling when app will refresh when returning from background

This is a rather general question on what actually happens when an app returns from background. I have a user telling me that on opening my app that he has not used for over a day, he will still be shown the same view that he had before he closed the app (by "closed" I mean he just hit the home button to send it to background, NOT hold and then tap the cross button to kill the app completely).
I was on the impression that if an app is restored from the background after a long time, it will be reloaded completely (showing splash screen and everything), as in the cases when I opened facebook or gmail app after I haven't used it for some time.
So my question is, do I have to implement a check somewhere on the period of time passed since my app was sent to background and reset everything when it goes over a certain threshold, or is that supposed to be handled by iOS itself... and of course if a user sent my app to the background and return after 2 minutes I would not want to refresh
You might have to handle that in your appdelegate methods by setting a timestamp. Since multitasking is enabled in iPhones, it will start from the same screen again. An app can be in background for a long time until user decides to kill the app.
Some of the delegate methods are
- (void)applicationWillResignActive:(UIApplication *)iApplication;
- (void)applicationDidEnterBackground:(UIApplication *)iApplication;
- (void)applicationWillEnterForeground:(UIApplication *)iApplication;
- (void)applicationDidBecomeActive:(UIApplication *)iApplication;
If you want to disable multitasking you can do that by setting a UIApplicationExistsOnSuspend key in application plist. But that will make the app quit immediately when user presses home button.
The decision on whether your app should refresh after some period of time in the background is completely up to you and the needs of your app. An app could sit in the background for weeks without being killed by the OS. Or it could be killed seconds after going into the background. It all depends on resources being needed for other running apps.
If you want logic in your app that makes it restart after 24 hours, for example, then it is up to you to write code to handle this. Save a timestamp when the app goes into the background. When it returns to the foreground, compare the current time to the saved timestamp. If enough time has passed, you need to update your UI to reflect whatever desired state you want to show the user.

Programming background behavior when iPhone is locked and user presses power button

Sorry, I am new to iPhone development and my google searches have failed me, so I have had to resort to posting a potentially idiotic question on SO.
I would like to write an app that, when suspended, performs an action when the user presses the home or power button (presumably to unlock the phone). I read the section in the iOS App Programming Guide's App States and Multitasking and the "Processing Queued Notifications at Wakeup Time" discusses handling queued events upon waking up. However, this isn't what I'm after.
I'd like to know if it is possible to:
From the phone sleeping state (I couldn't find a document for phone states, so I am talking about the case where the user presses the power button to turn off the screen), I would like my app to be ready to respond to the event where the user presses the power or home button (to unlock the phone)
I would like my app to respond to the event that occurs when the user unlocks the phone. I found an answer here that's close.
I don't want any funkiness when calls come in, get ignored, etc. :)
From what I can tell, it's a very gray area in the API around locking and unlocking, and I'd like to verify whether or not I'm wasting my time trying to do this.
It looks like I can use the accelerometer to detect when the phone is locked, but I also assume that I won't be able to count on this behavior in all future versions of iOS.
EDIT - I think I can handle the locking and unlocking requirements by assuming that the application has to be running at the time the phone is locked and unlocked, but I still cannot figure out if it is possible to determine #1 above, which is that the power button has been pressed and the unlock screen is displayed. Likewise, I'd like to know if the power button has been pressed again and is no longer displayed (i.e. screen is off).
All of the behaviors for your app being suspended/backgrounded are in the document you referenced. The AppDelegate will receive these messages and pass them on to whatever view you want to listen for them.
-applicationWillTerminate
-applicationWillEnterBackground
-applicationDidBecomeActive
-applicationWillResignActive
You can set up a notification observer in your view if you would like the view to be notified of any of these events. Then just override them or set up a custom method to do any work that needs to be done.
While the Apple docs might be a little foggy at first, all the information is in there to let you know which state your app will go to and how it will be handled. It is up to you to figure out what your app needs to do for each of these events. Hope this helps.

How display an alert/ (Rating alert) just before app going to close/ backward in iphone objectivec

In my window based iPhone application i have developed a rating alert using iRate to rate my application. it displays an alert view.
this alert was displayed when app launches.
But i need to display this alert just before the app going to close when a user has selected to press the home button to exit the app.
it displays only 0.5 secs so it is invisible so what to do .. i tried it in applicationDidEnterBackground & in applicationWillResignActive and in pplicationWillTerminate but no use
what to do thanks in advance...
You can't do this. When your app gets the applicationWillTerminate or applicationWillResignActive messages, your app has a limited amount of time to do cleaning up processes before the OS kills it. Displaying a message will delay the exiting of the app and so it isn't allowed - it will just display very quickly and then the app will be killed because it hasn't finished.
If a user has selected to press the home button to exit the app, they want to exit the app straight away. They don't want to exit the app some time after they've pressed it just because you want to show them some other things.
applicationDidEnterBackground: is the method you can use to know when you're app will be backgrounded. You only have 5 seconds to return from this method though so it may not be the best place to show an alert view
If you love your users, don't show rating alerts. Create a great app to get great ratings.

Iphone Calendar Application

i try to retrieve Calendar events between two dates. After that I try to delete all events. this program is working fine. The Events deleting process will take few second/minute based on number of events.
When device goes to sleep this application will work in background as usual. After coming to normal state(wake up) my application will exit. But, i did over come this problem, disable idle time.
But this same problem repeating when i press sleep/wake up button on the top of iPhone. How to over come this problem. What happens in my application, when changing state, any memory problem or any other...
When the sleep/wake button is pressed, your application is sent to the background/changes mode to inactive. Check UIApplicationDelegate documentation applicationWillResignActive: and applicationDidEnterBackground:. When this happens there are restrictions about your application doing background operations, which are described within the documentation.

Add a 'crash screen'?

I have an alarm app and it should never crash, but in the event that it does (especially in beta testing phase) is there a way to show a crash image? So an image that it goes to if the app crashes (but it stays in the app)? That way if it were worst case scenario and were to crash during the night, they may wake up, see the app's crashed screen and be saved from sleeping in.
If you use local notifications, and set one when the user sets the alarm, they'll get woken up matter what. If your app hasn't crashed then you can stop the notification from showing and raise the alarm in your own way. But if the app crashes or the user switches app then an alert view will pop up and you can set the sound to be made.