Iphone Calendar Application - iphone

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.

Related

In Corona SDK How to hide a group if application suspended?

I am building a word game and I want to hide the board when application is suspended?
the code looks fine however it givs a strange behaviour!!,
when I suspend the app nothing will happen but when i resume the application then the board will hide!!
local onSystem = function( event )
if event.type == "applicationSuspend" then
print("suspend")
board_group.alpha = 0
end
end
Runtime:addEventListener( "system", onSystem )
Note: you might wonder how do I know how the application looks when suspended?
the answer is: by pressing the home button twice.
example
SpellTower in normal state
https://dzwonsemrish7.cloudfront.net/items/430k0c0b0y0b413d0b42/Image%202012.11.12%208:08:24%20AM.png?v=4822f549
SpellTower after pressing the home button twice
https://dzwonsemrish7.cloudfront.net/items/280a1y0r2U3W321y1B2z/Image%202012.11.12%208:08:31%20AM.png?v=09c37567
you can see how they are hiding the letters, this is exactly what I want to do for my game, the only difference is i am using Corona SDK
When you do board_group.alpha = 0 you only has set a variable, the result will only take effect after a screen update.
But since the application is suspended... it won't update! So, changing any graphics on applicationSuspend don't work.
I believe the reason is because the application is not considered as suspended. In normal objective c programming it means that applicationWillResignActive is called when the user double clicks on the home button. So what you want to do is to add that code for this part.
Here is a flow of events:
http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/
Corona seems to have these events:
"applicationStart" occurs when the application is launched and all code
in main.lua is executed.
"applicationExit" occurs when the user quits the application.
"applicationSuspend" occurs when the device needs to suspend the application such as during a phone call or if the phone goes to sleep
from inactivity. In the simulator, this corresponds to the simulator
running in the background. During suspension, no events (not even
enterFrame events) are sent to the application while suspended, so if
you have code that depends on time, you should account for the time
lost to an application being suspended.
"applicationResume" occurs when the application resumes after a suspend. On the phone, this occurs if the application was suspended
because of a phone call. On the simulator, this occurs when the simulator was in the background and now is the foreground application.
So my guess is that you have to implement it outside of the corona API.
According to the corona documents you can implement them in the delegate:
You can intercept UIApplicationDelegate events via your implementation
of the CoronaDelegate protocol.
This protocol conforms to the UIApplicationDelegate protocol. Corona's
internal delegate will call your protocol's method if it is
implemented.
Please keep in mind the following:
Methods that Apple has deprecated will be ignored.
In most cases, your class' version will be invoked after Corona's corresponding version of the UIApplicationDelegate method. There is one situation in which your version will be called before.
In situations where the app is about to suspend or go to the background, your method will be called before Corona's version, e.g.
applicationWillResignActive: and applicationDidEnterBackground:.
http://docs.coronalabs.com/native/enterprise/ios/CoronaDelegate.html
But this is just a guess. Hope it helps!
Edit:
I was thinking, something really simple you could do is catch it outside and present a "pause" screen, then just hide it when the application enters foreground.
So if you can't do that (for now), one other option is to save application state when the application is about to terminate, and then set UIApplicationExitsOnSuspend = true in your plist file. This will cause the application to exit instead of suspending, which will avoid any screenshots, effectively "hiding" the board, etc. The downfall is, the app will have to read the session state when it launches again... this is only useful if your application can be designed to actually exit without losing your state, and is quite honestly, a little extreme. That said, it may be the only way to effectively do what you're trying to do.
Other ideas would be to see if you can add a large black layer to the screen, even though the application is suspending; perhaps this will somehow trigger an internal screen update by natively setting setNeedsDisplay. Also, instead of modifying the alpha, you might consider temporarily removing all of your layers and see if that has a similar effect.

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.

Crash when receiving call while playing audio

I am working on an online radio and it works fine, but gets problem when user received a call. When call finishes I start the radio automatically, by using AVAudioSessionDelegate.
Now radio is playing and if user open the app (moving it to foreground) and press that stop/play button again my app goes crashes. I might know the reason, the reason may be due to threading. But I am unable to handle this (I can't put the code, it is huge and private).
To prevent crash when your app goes in background and comes in foreground, call the pause button of radio app in background and releases threads. when user touches the play button it it will start threading. Try it,hopes it may work for you.

UILocalNotification handling when screen is locked

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.