tags:

views:

938

answers:

3

My iPhone app (well, idea of it) needs to do changes to iPhone's filesystem. Does iPhone API allow that?

+7  A: 

Your app has its own piece of filesystem that you can read from and write to but you can't access anything outside that, ie you cannot access the filesystem areas of other apps or the OS itself.

Benno
A: 

i just wrote a tutorial on how to do this using NSMutableArrays that you can check out that should help you. http://idevkit.com/iphonedev/2009/09/saving-nsmutablearrays/

if you have anymore questions on it lemme know and ill add to it

Sj
+2  A: 

Yes Cocoa Touch for iPhone OS allows file access. With the caveat that it naturally only allows file access for files that the current user has read-write permissions to access. Each application runs as single user, and really only have access to it's own small sandbox of files. So you will not be able to access system files, or files from another application.

There are two main directories that you might want your app to access:

  • NSDocumentDirectory - Analogous to you own Documents folder, the contents of this folder is backed up when you synch the device.
  • NSCachesDirectory - This one resides in /Library/Caches/ and is not backed up when synching the device.

You get the path to these directories using the NSSearchPathForDirectoriesInDomains function. When searching you will get an array of potential paths, it is safe to use the first path as long as you only search in the user domain. For example:

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

Once you have a path you can work away with your files using the default file manager. For example remove a file:

NSFileManager* fm = [NSFileManager defaultManager];
[fm removeItemAtPath:filePath error:NULL];
PeyloW