Search code examples
iphonenskeyedarchiver

How to read a .data file encoded using NSKeyedArchiever iPhone


I use NSKeyedArchiver to archive data in my iPhone app into a .data. Long story, short, I lost all my data. I use "iPhone back up extractor" and it works great. I found a .data file for my app.

To be clear, I found a .data file and I would to open and see what the contents look like. I understand no software (Ex: notepad) might be able to read that file and I may have to use Xcode. If you know of any way, Xcode/notepad/someProgram, please let me know.

This is the code that I implement when I build the app:

- (NSString *)locPath {
    return pathInDocumentDirectory(@"tableArray.data");
}

- (void)archieve{
    // Get the path to the document directory
    NSString *path = [self locPath];

    // grab the array
    NSMutableArray *tableArray = [someViewController tableArray];

    // archive the array to file
    [NSKeyedArchiver archiveRootObject:tableArray toFile:path];
}

This code is called during applicationWillTerminate, applicationDidEnterBackground, etc...

The data is restored/called upon in the didFinishLaunchingWithOptions like this:

// Get the path to the document directory
NSString *path = [self locPath];

// Unarchive .data into an array
NSMutableArray *tableArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

Any help is appreciated! Thanks!!!

PS: Link for iPhone back up extractor is http://supercrazyawesome.com/


Solution

  • The easiest way I found is to get the document directory using this code:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *dir = [paths objectAtIndex:0];
    NSLog(@"Directory: %@", dir);
    

    In there, the Documents dir will contain the .data file