ApplicationWillTerminate in iOS 4.0 - iphone

The applicationWillTerminate delegate method is not getting called in iOS 4.0
When I hit the Home button I am seeing the applicationWillResignActive and applicationDidEnterBackground delegate methods getting called.
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(#"Application Did Resign Active");
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"Application Did Enter Background");
}
And when I double Tap the Home button and again launch the Application the i find the applicationWillEnterForeground and applicationDidBecomeActive delegate methods are getting called.
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"Application Will Enter Foreground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(#"Application Did Become Active");
}
But I want to know when the applicationWillTerminate delegate method will be called , where I do some DB/file backup routines.
- (void)applicationWillTerminate:(UIApplication *)application{
}
I even tried to hit the minus sign and deleted the App running in the Background , but still it did not call any delegate method.
Any Ideas ???

From the iPhone Application Programming Guide:
Even if you develop your application using iPhone SDK 4 and later, you must still be prepared for your application to be terminated. If memory becomes constrained, the system might remove applications from memory in order to make more room. If your application is currently suspended, the system removes your application from memory without any notice. However, if your application is currently running in the background, the system does call the applicationWillTerminate: method of the application delegate. Your application cannot request additional background execution time from this method.
So yes, applicationWillTerminate: will generally not be called very often in iOS 4. If you have to save data, you should do so in both applicationWillTerminate: and applicationDidEnterBackground:.

The WWDC 2010 Session Adopting Multitasking on iPhone OS (Part 2) explains the application state transitions extremely well.

I got one solution for terminating apps when user hits the Home button in iOS4.
This will call the applicationWillTerminate delegate method instead of entering into background process.
Open your info.plist file
Add The Key UIApplicationExitsOnSuspend
Set the new key to YES

Actually you can also use this step to do so.
Open your info.plist file
Add The Key -> Application does not run in background
Set this key value to YES

Related

Detect app opening?

I have a Tab bar app with 3 view controllers and I use "viewWillAppear" to detect which view is opend.
When I close the app, it still works "in the background" and when I open the app again "viewWillAppear" is not detecting this opening.
Is there any other option to detect this opening? Thanks
You can observe the UIApplicationWillEnterForegroundNotification or implement applicationWillEnterForeground: in your app delegate.
Firstly, You should see the necessary delegation method in UIApplicationDelegate
When you close application that currently open, It will call this method:
- (void)applicationDidEnterBackground:(UIApplication *)application
After the application has been closed but still in dock, you open them again. In the transition state before entering the application, It will call this method:
- (void)applicationWillEnterForeground:(UIApplication *)application
When the application completely presented on previous state before you closed them. It finally call thid method:
- (void)applicationDidBecomeActive:(UIApplication *)application
If you would like to do something in viewWillAppear you should implement in applicationDidBecomeActive to send some message to your current view or other to do what do you want to do after application became actived.
When your app is resumed from the background, it will receive the applicationWillEnterForground: method. It'll get the applicationDidEnterBackground: when it is suspended too.
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(#"app will enter foreground");
[viewController refresh:NULL];
}
i think this will work.
write this in your app delegate

How can I know whether app is terminated by user?

How can I know whether app is terminated by user?
(Double-clicking Home button and press red minus '-' button)
I need sample or pseudo code...
Sometimes you do get the UIApplicationWillTerminateNotification (you can also implement the applicationWillTerminate: method in your app delegate), but it's not guaranteed that you do get any notification at all in this case. So don't rely on it.
You won't receive the notification if your app is "suspended", that is it is in background but not running. In the suspended case you simply get killed with the non-catchable SIGKILL. Same for when you get killed due to out-of-memory.
From the documentation for applicationWillTerminate: (emphasis mine):
For applications that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the application. For applications that support background execution, this method is generally not called when the user quits the application because the application simply moves to the background in that case. However, this method may be called in situations where the application is running in the background (not suspended) and the system needs to terminate it for some reason.
When user kills the app:
- (void)applicationWillTerminate:(UIApplication *)application{
}
Application will enter inactive state:
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(#"Application Did Resign Active");
}
User pushes Home button:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"Application Did Enter Background");
}
Try this:
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}
You should implement the method above in your class that implements UIApplicationDelegate
Some more notes: that method won't be called if you are stopping your app via Xcode (stop debug button). So be patient when testing.
Also there is this:
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
Could be helpful if you want to know did it enter background ;)

What is the Exit point of iphone application

Please any one can tell me what is the exit point of application?
I have developed an app, in this app I have passes an array in all views(used in whole application), an its running perfectly.
But I want to release this array when app exits.
Thanks.
In iOS with multitasking, you don't have a clear exit point. You application can be killed at any time without notice if it is in the background. So if you have settings to save, you need to do so in the app delegate methods applicationWillTerminate: (iOS without multitasking) and applicationDidEnterBackground: (iOS with multitasking). It's also a good idea to save on applicationDidResignActive:. See the UIApplicationDelegate reference.
Note that all of these events also post NSNotifications to which you can subscribe to in any class you like. See the notifications section in the UIApplication reference.
As for releasing your array: you should release all your data in your classes' dealloc methods (yes, same applies for the app delegate).
- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
}
this in your app delegate class.
The dealloc in your App delegate should do it. Just add
- (void)dealloc
{
[yourArray release];
}
in your app delegate....
The array will be automatically released when the app exits, as the system reclaims all the app's memory. You don't need to worry about it.

Using the applicationWillTerminate method doesn't seem to be working

I am wanting to save some app state data for when the user is quitting the app. I am thinking that I should be using the applicationWillTerminate method inside of my appDelegate, but when I try and do anything there, it's not doing anything:
- (void) applicationWillTerminate:(UIApplication *)application {
NSLog(#"test");
}
When I run my app, and do some stuff, and hit the home button to quit the app, nothing comes over the console...
Do I have to implement the applicationWillTerminate method in the appDelegate? The user of my app will most likely be in a Reader view when they leave, is there anyway to implement the app will close method there?
see this link iPhone simulator and applicationWillTerminate() for older iOS versions;
but remember the Home button does not necessarily terminate the application is ios4... you should use applicationDidEnterBackground but would suggest both places applicationWillTerminate and applicationDidEnterBackground
Adding the UIApplicationExitsOnSuspend key to your application’s Info.plist file and setting its value to YES will make you application quit when you hit the home button, even on iOS4
In iOS 4, the applicationWillTerminate: method is only called if you opt out of the background execution model. In the standard iOS 4 application lifecycle, applications are suspended (not terminated) when the home button is pressed. Your application delegate will receive an applicationWillResignActive: message followed by an applicationDidEnterBackground: message. The applicationDidEnterBackground: method is a good place to save user data.

Is it possible to logout when the Home button is pressed?

Is it possible to logout when the iPhone Home button is pressed?
In the current iPhone API, it is impossible to "hijack" any of the hardware presses. You will be notified of certain events however. If you are wishing to call some function (logout) when the user exits the app by pressing the home button, you can implement the
- (void)applicationWillTerminate:(UIApplication *)application {
....
}
method in your Application Delegate. This method is called when the app is about to terminate through a phone-call, manual exit, or any other reason. You should be advised, however, that anything here better be very short execution and non-essential, as Apple does not guarantee that it will run through the whole method before terminating the application
For iOS 4.0 + however you will have to implement logout in this method as well
- (void)applicationDidEnterBackground:(UIApplication *)application