1

What is the correct/best way to store files on the iphone?

I would like to save a file in some directory on the iphone. I have read that filing the users home directory isnt good practice.

Where should the files be stored and how can an URL for these files be saved as well? So that after saving a number of files to the users phone, the urls can be stored so that the files can be managed at a later stage.

I would like to store files, keep a handle to the file and store these handles in a data structure as well as retrieve the files later using URLs in the data structure so that the files can be accessed and deleted.

What would be the correct/optimal way to do this?

some_id
  • 29,466
  • 62
  • 182
  • 304
  • You should choose best directory depending on the type of information you're saving. See my [answer](http://stackoverflow.com/a/12472910/991816) – DanSkeel Jun 02 '13 at 11:51

1 Answers1

-2

Files are stored in Documents folder of the App.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
[paths lastObject];

will get you the Document path.

I would then use NSKeyedArchiver and NSKeyedUnarchiver to store the URLS, and also put that into the Documents.

To see what's in the Documents Directory just use NSFileManager contentsOfDirectoryAtPath:error: method, which returns a handy NSArray. Call lastObject and there you go.

Update: NSURL must be encoded and decoded with NSKeyedArchiver and NSKeyedUnarchiver, as they are not a basic object of plists. Although you could could convert them to strings, which are, then you could use [array writeToFile:file atomically:YES]. The other option is CoreData but that might be a bit much.

It would be best to store all URLs in one file and just overwrite and with changes when urls are being removed and added.

You are not filling the user's Document directory, you are filling the app's document directory. The documents folder is where you but files that the user has generated. In the future you may decide to open up the Document directory to iTunes so the user can add and remove files that way. Things that just relate the app should be stored in the Library folder.

I hope this explains what the hell I was on about a little better. I seemed to got the impression that you wanted to just store URLs as individual files and store the handles. You could get the handles but just knowing the directory you stored everything in. Get an array of the files in that folder and just load them up by using an unarchiver or initWithFileContents: from NSDictionary or NSArray.

NebulaFox
  • 7,813
  • 9
  • 47
  • 65