Can I save the state of the application in the near future on the iPhone OS?

I have an application that works like an e-book. I have a bunch of textual information in different languages, which is available using several drilling methods. When a user wants to get to where they read the latter, they currently have to navigate sections and chapters in order to return to where they were. An ideal solution for this would be to create a bookmarking system that I am considering.

But if I remember correctly when I announced iPhone OS 4, they seem to have added the ability to save the state of the application. Does this mean that someone using my reading application can just go straight out, do something, and then when they return, it will be a screen to read as soon as they leave it?

I don’t know much about how to set up the bookmark system, I believe that it would be interesting to explore, but I would probably just want to hold on to iPhone OS 4, if that is really what it can do. Any thoughts or ideas will be appreciated!

+3
source share
4 answers

iPhone OS. NSUserDefaults . -applicationDidFinishLaunching , , , , NSUserDefaults.

+5

, OS4, .

, , , , NSUserDefaults :

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

, .

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

, - , ( )


, , " ". , NSMutableArray, , NSUserDefaults . , , , .

+5

, , . , ebook , , 3. NSUserDefaults .

+3

if you use a navigation controller, you can use the following line of code to save the state of the application:

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 the application has entered the background and you can use this line of code to restore your application:

 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 {

        }
    }
 }

during the application finished loading

+3
source

Source: https://habr.com/ru/post/1742124/


All Articles