开发者

Can I save my app's state at close time in iPhone OS?

I have an app that functions much like an ebook. I have a bunch of textual information in various languages that is accessible through a number of drill down methods. When a user wants to get into where they were reading last, 开发者_StackOverflow社区they currently have to navigate through the section and chapter menus to get back to where they were. An ideal solution for this would be to setup a bookmark system, which I am considering.

But if I remember correctly, when iPhone OS 4 was announced, they seemed to make a big deal of the added ability to save the state of an app. Does that mean that someone using my reader app would be able to just exit right out, do whatever, and then when they came back in, it would be the reading screen just as they left it?

I don't know much about how to setup a bookmarking system, I suppose it would be worth investigating, but I would probably want to just hold off for iPhone OS 4 if that is indeed what it will be capable of doing. Any thoughts or insights would be appreciated!!


This has always been possible in the iPhone OS. Most people use NSUserDefaults to store their application's state. Then in your application delegate's -applicationDidFinishLaunching method, or wherever you set up your views and initialize your application state, simply restore the state that you saved using NSUserDefaults.


As others have pointed out this is not particular to OS4 and it is good practice to implement this in all apps.

I would suggest that when a user turns a page in a book for example you save a reference to that page using NSUserDefaults as follows:

NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
[userPreferences setObject:currentPageNumber forKey:@"pageNumber"];

When the application launches you can read this value and act accordingly.

[[NSUserDefaults standardUserDefaults] stringForKey:@"pageNumber"];

You may prefer only to store the reference when the application is about to close but it is better practice to store it earlier (in case the application quits unexpectedly)


Also, you mention that users navigate through a number of "drill down methods". You may want to keep a record in a NSMutableArray of where exactly the user is in the app and save this out to NSUserDefaults too. When your app launches you would process the information in this array to get the user back to where they left off.


What they meant for save state is for background application. A ebook app probably won't need to run in the background so you'll still have to save and restore state just like in OS 3. NSUserDefaults works well for this.


if you are using navigation controller then you can use the following line of code to save you app state:

NSMutableArray *listOfClasses = [[NSMutableArray alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:@"ViewControllers"];
NSArray *controllers = [_navController viewControllers];

for(UIViewController *VC in controllers)
    [listOfClasses addObject:[NSString stringWithFormat:@"%@",[VC class]]];

[defaults setObject:listOfClasses
             forKey:@"ViewControllers"];
[defaults synchronize];

at the time of termination of application did enter in background and you can use these line of code to restore your app:

 NSArray *classes = [[NSUserDefaults standardUserDefaults] objectForKey:@"ViewControllers"];
if(classes != nil) {
    for(NSString *class in classes) {
        if([class isEqualToString:@"FirstViewController"]) {
            FirstViewController *viewController  = [[FirstViewController alloc] init];
            [controllers addObject:viewController];
            [viewController release];
        } else if([class isEqualToString:@"SecondViewController"]) {
            SecondViewController *viewController  = [[SecondViewController alloc] init];
            [controllers addObject:viewController];
            [viewController release];
        } else if([class isEqualToString:@"ThirdViewController"]) {
            ThirdViewController *viewController  = [[ThirdViewController alloc] init];
            [controllers addObject:viewController];
            [viewController release];
        } else {

        }
    }
 }

at the time of application did finish loading

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜