tags:

views:

117

answers:

3

So I know iOS apps run in a sandbox and all, but I thought I remembered Apple mentioning a way for apps to write to and read from a common file share, so that you can then deal with these files on your computer when you plug you iPhone in (mount the drive). I think this was in the context on the iPad, but I'm not sure.

Anybody know anything more of this? It's kind of a tough thing to search for.

+1  A: 

You'll want to store and retrieve files from the /Documents directory. It is for storing application specific data. If you add the UIFileSharingEnabled key to your app's Info.plist and set it to YES, then your users will be able to see the list of documents in the "Applications" tab of iTunes when their device is connected to the computer and, from there, can add, remove and copy said documents.

UIFileSharingEnabled is documented here (and, yes, it was hard to find).

bbum
+1  A: 

iOS 3.2 and later support file sharing.

UIFileSharingEnabled (Boolean - iOS) specifies whether the application shares files through iTunes. If this key is YES, the application shares files. If it is not present or is NO, the application does not share files. Applications must put any files they want to share with the user in their /Documents directory, where is the path to the application’s home directory.

In iTunes, the user can access an application’s shared files from the File Sharing section of the Apps tab for the selected device. From this tab, users can add and remove files from the directory.

This key is supported in iOS 3.2 and later.

http://developer.apple.com/iphone/library/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW20

Shaji
+3  A: 

Each app has a Documents directory where it can write files. To get the apps Documents directory you can use:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

To write say "Hello, World!" to a file called hello.txt in the documents directory you could do something like:

NSString *hello = @"Hello, World!";
NSString *myFile = [documentsDirectory stringByAppendingPathComponent:@"hello.txt"];
[hello writeToFile: myFile atomically:YES encoding:NSUTF8StringEncoding error:NULL];

If you set UIFileSharingEnabled in your app's Info.plist to YES the the files will be visible in iTunes when the device is connected and can be accessed by the user from there.

Jason Jenkins