Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1099205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:41:55+00:00 2026-05-17T00:41:55+00:00

Let’s say that my application state is extracted into an object (so that all

  • 0

Let’s say that my application state is extracted into an object (so that all information specific to app instance is contained in one object) and that object supports nscoding protocol. How can i easily persist it and load it on exit/launch of my application?

My current code looks like this

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    State* state = (State*)[defaults objectForKey:@"State"];
    if(state!=nil)
    {
    viewController.state = state;
    }
}

- (void)applicationWillTerminate:(UIApplication *)application {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.viewController.state forKey:@"State"];
}

But upon loading state is always nil… so I assume it is not the best pattern out there 🙂


It turns out that NSUserDefaults supports only Property List objects such as NSArray, NSData, etc… no custom objects unless you wrap it in nsdata

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:41:56+00:00Added an answer on May 17, 2026 at 12:41 am

    There are a number of ways of storing the state of your application before it exits and restoring it when it launches again. The best approach really depends on your application.

    If you have a simple object model with a single root object and you tend to need the entire object model in memory at once, then you may find the NSCoding protocol (see link below) a good way of saving state.

    http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Protocols/NSCoding_Protocol/Reference/Reference.html

    Any of the objects you want to persist should implement this protocol’s two methods:

     - (void)encodeWithCoder:(NSCoder *)aCoder // to serialise the object
     - (id)initWithCoder:(NSCoder *)aCoder // to restore an object
    

    Once you’ve implemented these methods in any of the objects you want to persist, you can serialise your object graph with something like this:

    - (void)applicationWillTerminate:(UIApplication *)application {
      [NSKeyedArchiver archiveRootObject:yourRootObject toFile:someFilePath];
    }
    

    When you want to restore the archived objects:

    - (BOOL)application:(UIApplication *)application
         didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
      id yourRootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:someFilePath];
    }
    

    Alternatively, if your application’s object model is slightly more complex you might consider using Core Data.

    http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html

    Finally, whilst the user defaults system can be used for storing and retrieving values, it is best used for simple application settings (or preferences). It isn’t really intended for saving your entire application’s state.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.