Retain the State of iPhone Application after exit - iphone

How can I retain the state of an iPhone after it has exited. What is the easy way to do it?

The first question is when do you save? The answer is in two places (assuming you want to support 3.x and 4.x devices).
First, for OS 3.x devices (and OS 4 devices that don't multi-task):
- (void)applicationWillTerminate:(UIApplication *)application
And second, for OS 4.x devices:
- (void)applicationDidEnterBackground:(UIApplication *)application
You need to do this on iOS4 devices because if the app is shutdown while it's in the background it is just killed; you never see the applicationWillTerminate message.
As for the how, well it depends on how complex your app is. I created a simple protocol that I implement for each view controller that might want to save its state:
#protocol SaveState
- (NSData*) saveState;
- (id) initWithSaveState:(NSData*)data;
#end
It saves the state by looping through view controllers in the main navigation controller and calling the save state method. It then does the reverse in the applicationDidFinishLaunching: method. More information on my blog.

In your application delegate, you can define the -applicationWillTerminate: method to include code to save application state data.
- (void) applicationWillTerminate:(UIApplication *)application {
// save state to data model here...
}
Your data model is up to you. For example, this could be a set of user defaults or a Core Data store.
The next time the app is started, you could check for saved state data in -applicationDidFinishLaunching: and initialize the app appropriately.
If you are using iOS 4 and your application supports multitasking features, you will get some of the state saving functionality "for free" because the app resigns focus, instead of terminating.

Related

Xcode default one page ios project - Why isn't the delegate responsible for loading the controller

I'm new to xcode and ios development. I'm learning using Brad Larson online courses. Now, he doesn't use storyboard to create interface. In his delegate file, he's using code for loading the rootViewController.
Now, when I create a new one page ios project, its seems like it's not the XYZAppDelegate that is responsible for loading the controller.
I know that in the mainstoryboard.storyboard file, my XYZViewController appears in there. I'm just wondering why it's not the XYZAppDelegate that is in charge of loading the viewController ?
In the appDelegate, there is nothing in the didFinishLaunchingWithOptions method but the program still loads?
However, the main.m file indicates that XYZAppDelegate is the delegateClassName.
Finally, my question is who's in charge in that case for loading the XYZViewController if it's not the AppDelegate ?
These are the default xcode generated files
main.m
return UIApplicationMain(argc, argv, nil, NSStringFromClass([XYZAppDelegate class]));
XYZAppDelegate
#import "XYZAppDelegate.h"
#implementation XYZAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent 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 or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
#end
The answer lies in your Info.plist file. If you look, you'll see two keys called Main storyboard file base name and Main storyboard file base name (iPad) for which the values should be the name of your storyboard files for the respective devices. If these keys are present at launch, the OS will automatically load the storyboard file and insert the first view controller from the storyboard into the app's window. Docs here for the Info.plist key explanations (the raw key is called UIMainStoryboardFile).

How to handle phone call interrupts in iPhones below iOS 4

In one of my application I need to handle call interrupt. I know application will directly enter background in iOS 4 onwards. How can I handle this situation devices with iOS's less than 4. Do I need to implement any delegate method to be get notified that a call interrupt has come. Please help.
Thanks in advance.. :)
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 or when the user quits the application and it begins the transition to the background state.
To Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. For this use the below appDelegate
(void)applicationDidBecomeActive:(UIApplication *)application
You can have a look at The Application Life Cycle. You will find there the difference between iOS 4.0 and earlier versions.

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.

iPhone Application Themes

I tried using a multi-value settings bundle to change the view. I would do the if statements in the applicationdidfinishloading in the application delegate. Apparently the method isn't called every time the app is loaded, and it would not work correctly.
If anyone has done this, or has any suggestions, links to tutorials. I would really appreciate it. I'm just trying to load views (nibs) based on user preference.
I think you can put your code in
- (void)applicationDidBecomeActive:(UIApplication *)application
or
- (void)applicationWillEnterForeground:(UIApplication *)application
methods also because from iOS 4.0 due to multitasking your app is just in the background state so it wont call applicationdidfinishloading method when the user presses the icon of your app again.

ApplicationWillTerminate in iOS 4.0

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