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 6951695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:15:10+00:00 2026-05-27T14:15:10+00:00

I know this sounds like an odd question, but I need to keep a

  • 0

I know this sounds like an odd question, but I need to keep a copy of my NSUserDefaults in to a database (my aim is provide a database backup / restore feature, using one file, the database).

So I think I’ve figured out how to load to a file (although I haven’t tried this in xcode).

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults registerDefaults:[NSDictionary dictionaryWithContentsOfFile:
   [[NSBundle mainBundle] pathForResource:@"UserDefaults" ofType:@"plist"]]];

I’ve googled how to save NSUserDefaults to a plist and to a string and back, but haven’t found anything.

  • 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-27T14:15:10+00:00Added an answer on May 27, 2026 at 2:15 pm

    You can use the asynchronous NSPropertyListSerialization API or just the synchronous convenience methods on NSDictionary.

    Checkout the discussion in the NSDictionary Apple Docs on the writeToFile:automatically method for more info on how it works

    Also, This article has some good info on Serialization in cocoa generally.

    Use the following code should get you on your way.

    //Get the user documents directory
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    //Create a path to save the details
    NSString *backedUpUserDefaultsPath = [documentsDirectory stringByAppendingPathComponent:@"NSUserDefaultsBackup.plist"];
    //Get the standardUserDefaults as an NSDictionary
    NSDictionary *userDefaults = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
    
    //The easiest thing to do here is just write it to a file
    [userDefaults writeToFile:backedUpUserDefaultsPath atomically:YES];
    
    //Alternatively, you could use the Asynchronous version
    NSData *userDefaultsAsData = [NSKeyedArchiver archivedDataWithRootObject:userDefaults];
    
    //create a property list object
    id propertyList = [NSPropertyListSerialization propertyListFromData:userDefaultsAsData
                                                       mutabilityOption:NSPropertyListImmutable
                                                                 format:NULL
                                                       errorDescription:nil];
    //Create and open a stream    
    NSOutputStream *outputStream = [[NSOutputStream alloc] initToFileAtPath:backedUpUserDefaultsPath append:NO];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    outputStream.delegate = self; //you'll want to close, and potentially dealloc your stream in the delegate callback
    [outputStream open];
    
    //write that to the stream!
    [NSPropertyListSerialization writePropertyList:propertyList 
                                          toStream:outputStream 
                                            format:NSPropertyListImmutable 
                                           options:NSPropertyListImmutable 
                                             error:nil];
    

    When you want to go backwards you can simply do something like:

    NSDictionary *dictionaryFromDisk = [NSDictionary dictionaryWithContentsOfFile:backedUpUserDefaultsPath];    
    

    Or you could use the stream/NSData approach from NSPropertyListSerialization, which is similar to the way you save it.

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

Sidebar

Related Questions

This sounds like a trivia question but I really need to know. If you
I know this sounds like a really obvious question, but it's proving harder to
I know this sounds like a broad question but I can narrow it down
I know this sounds like a dumb question, but it's to settle an argument
I know this sounds like a stupid question, but here it is: Is there
I know this sounds like a homework assignment, but it isn't. Lately I've been
I know the question sounds silly, but consider this: I have an array of
This might sound a bit of an odd question but I know what I
Hi I know this sounds quite not practical but still I would like to
Sorry if this sounds like a really silly question. But I Googled the web

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.