Which function is called when iPhone app is terminated? - iphone

I'm working on a project in Xcode version 4.2.1 and in iOS 5.0. I'm wondering which function will be called when you completely terminate an application, which means even the time when an users force the app running on background to be terminated from home screen. I was assuming that
(void)applicationWillTerminate:(UIApplication *)application
would be called, but it turned out it was not called even after terminating the app on background.
What I basically want to do is to call a method that I created just before the app is completely terminated, so need to know which function is called at the end of the app.
Thank you.

"applicationWillTerminate" method will be called only when application is not in suspended state while being terminated.
To understand this, considering currently your application is in foreground active state, go through following two cases:
Case 1: When you single tap Home button, app directly goes to suspended state by calling methods - (1) applicationWillResignActive and then (2) applicationDidEnterBackground.
Now when you try to quit/terminate app, by double tapping home button and then swiping up the current app screen from recent app screens, app is in suspended state. So, "applicationWillTerminate" method will not be called.
Case 2: When you double tap Home button, app goes to inactive state by calling method - (1) applicationWillResignActive.
Now when you try to quit/terminate app, by swiping up the current app screen from recent app screens, app is in inactive state (not in suspended state) while being terminated. So, "applicationWillTerminate" method will be called.
Look what Apple say:
For more info on this look - Apple's Official Documentation on applicationWillTerminate(_:)

Here's a good overview of the application lifecycle notifications & delegate messages on iOS 4.0 and newer. In short...
Your app will generally never see willTerminate, because the system generally only terminates your app once it's already suspended (in the background). Once your app is suspended, it gets no further chance to act(*), so there's no callback for that.
The didEnterBackground delegate message or notification should be considered your last chance to clean things up or save state before possible termination.
(*) Okay, your app can do stuff if it's in one of the supported background execution modes, like audio, VoIP, or navigation, but in that case it either hasn't been suspended yet or it's been un-suspended with an entry point specific to that background mode.

If the app is already suspended, it will not receive further notifications. Watch for the didEnterBackground notification.

- (void)applicationWillTerminate:(UIApplication *)application
It's in the UIApplicationDelegate protocol.

The documentation here spells out the behavior for this case.
In short, you'll only receive the applicationWillTerminate: message if the app is running (either in the foreground or background) when it's terminated. So, unless your app is running in the foreground or in one of the long-running background modes (e.g. VoIP, Audio, or a long-running task indicated by beginBackgroundTaskWithExpirationHandler) it will transition from the background state to the suspended state fairly quickly. In this case, you'll never receive the applicationWillTerminate: message.

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.

How to handle the phone call while running the application in iphone

I am doing with a paint application. while drawing any picture if I got any phone call, the application is getting quit and reopens immediately after end of the call. But my requirement is I should able to draw the picture while I am in incoming call.
Any one can Please help me out
When a phone call comes in, and the user picks it up, they will always leave the current app and the phone app will start. I don't think you can change this behaviour.
While the user is in the phone call, though, they can change to another app simply by pressing the home button and starting another app from the home screen, or by double-pressing the home button and switching to another app, including yours.
In that case there will be a thin view below the status bar indicating that they're in a call, so you have to make sure your app resizes properly to make space for that.
This is default from apple that any application go inactive if incoming call is in active.We cannot change this.
Use the below Appdelegate method:
(void)applicationWillResignActive:(UIApplication *)application
This delegate will call when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call
)
To Restart any tasks that were paused (or not yet started) while the application was inactive. For this use the below appDelegate
(void)applicationDidBecomeActive:(UIApplication *)application
For more visit app multitaking
Hope, this will help you..

what method in my AppDelegate.m is executed when an app is relaunched from background?

I have an app that has a local database and i wish to call a function when the app is called from background(i.e. when i recall it from the 'task manager') so that i may update my database from a web service. What is the method called and can i get it to execute in my app's delegate?
Check applicationWillEnterForeground: and applicationDidBecomeActive methods in UIApplicationDelegate.
applicationWillEnterForeground
In iOS 4.0 and later, this method is called as part of the transition
from the background to the active state. You can use this method to
undo many of the changes you made to your application upon entering
the background. The call to this method is invariably followed by a
call to the applicationDidBecomeActive: method, which then moves the
application from the inactive to the active state.
applicationWillEnterForeground & applicationDidBecomeActive method/delegate is called when application is restored from background.
However, you should keep this provision (i may update my database from a web service) in applicaitonDidFinishedLaunching as well because you dont know if OS have terminated your background application (which OS can certainly do upon his discretion if your app does not have proper background processing permissions) application and OS may launch the application as fresh launch rather than restoring from background.

How to know app is about to kill in iOS4.x?

I have an app with locationServices on in background. and have some back ground handle code also.
All I want that when user double click the home button and kill my app I should get notify.There is no application delegate method that is called every time (100%). though the method applicationWillTerminate: according to apple will be called on terminating the app but it is called in very rare cases ( 5 times in 100).
Does any body has some idea about it?
Did you consider implementing applicationDidEnterBackground: method? The doc says that, "In iOS 4.0 and later, this method is called instead of the applicationWillTerminate: method when the user quits an application that supports background execution".
When an app is hidden using the home key, the operating system will suspend it in memory. When this happens, applicationDidEnterBackground: is called. If, at that point, the system decides it needs memory, it will start close apps that are in the background. Sometimes, your app will be on that "hit list". When your app is killed, it is removed from memory completely, just after the pplicationWillTerminate: method is called. The method doesn't get called all the time because sometimes, iOS doesn't need the memory and so it won't actually terminate your app, but rather it will suspend it.
Hey I found a very nice observation, that if your App is running some code in background and user tries to kill it the method applicationWillTerminate is called.

How to Terminate a App on a custom Case. iOS4

My App needs a Internet Connection which is checkt in the ViewDidLoad if there is no Internet connection I want to terminate the app when the Home Button is clickt so that the app start in its initial state next time but only in this case.
If there is a Internet connection from the start the homeButton should bring the app just to the background.
Apple does strongly discourages quitting from application programmatically.
I think you can handle your case without quitting application - when application goes to background(in applicationDidEnterBackground method of application delegate) save some flag indicating that you want to reinit it on resume, then when application comes back to foreground (applicationWillEnterForeground method in delegate) apply your initialization logic in case flag is set.
Programatically terminate an App is a behaviour that will be rejected to be published in the AppStore, as it seems that the application crashed.
If you don't mind that your application will never see the light at the AppStore, you can simply use exit(0).
When your app does not find an internet connection, switch to a view that exactly imitates your Default start-up image, then force that view to stay visible the usual amount of start-up time after any call to making your app active. You can also kill and recreate fresh all your other MVC objects during this time.
That way, no one will know that your app wasn't freshly started when brought back from the background.