Home | About | Apps | Github | Rss

Make files visible in iOS iTunes App Documents

For saving configuration files one should use the Application Support directory provided by the sandbox. The sign that you are using it is the flag NSApplicationSupportDirectory.

But to make your file visible in the iTunes App Documents directory

NSFileManager *defaultManager = [NSFileManager defaultManager];
NSError *error = nil;
NSURL *appPath = [defaultManager URLForDirectory:NSApplicationSupportDirectory
                                        inDomain:NSUserDomainMask
                               appropriateForURL:nil
                                          create:YES
                                           error:&error];

And for the file path to Documents and making it visible in iTunes

// Write to documents
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
NSString *fileName = @"mydocument.xml";
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

Additional code for storing using KeyArchiver in XML format. This is especially useful for debugging configs that you are writing to disk.

// Using key archiver store it
NSMutableData *mutableData = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];
[archiver setOutputFormat:NSPropertyListXMLFormat_v1_0];
[archiver encodeObject:objectToBeEncodedforKey:@"root"];
[archiver finishEncoding];
[mutableData writeToFile:filePath atomically:YES];

More posts