Thoughts, stories and ideas.

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

  • Enable the flag boolean UIFileSharingEnabled in Project-Info.plist. In xcode this is available in the dropdown as Application supports iTunes file sharing flag
  • Use NSDocumentDirectory flag instead of NSApplicationSupportDirectory

Here is an example for computing path to ApplicationSupport

NSFileManager *defaultManager = [NSFileManager defaultManager];
NSError *error = nil;
NSURL *appPath = [defaultManager URLForDirectory:NSApplicationSupportDirectory

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];