views:

62

answers:

1

Hello all,

So as a learning exercise, I am trying to make a simple file browser that interfaces with a file storage mechanism. (Think dropbox or box.net) I want to add a feature that would allow the user to flag a file for local storage so they could view it when they were not connected to the network. Is there an apple API that allows for something like that.

Perhaps there is a way to add the documents to the local bundle and then access the files that way at a later time? I haven't been able to find much documentation on this. Any insight, guidance or just general advice would be greatly appreciated. Thanks!

+2  A: 

You can find the application's document directory thus:

/**
 Returns the path to the application's Documents directory.
*/
- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

Then get the URL (i.e. pathname) of a file within that directory:

NSURL *fileURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent:theFilename]];

and use Cocoa file-writing methods (e.g. of NSString, NSData), or you can just use ordinary stdio and so on.

David M.
Right, I get that. That would be useful if I had a static file saved in the app directory. The problem is I need to figure out how to save one first. I have one in memory, but need to write it to disk to be loaded up later...
Geoff Baum
What class are you using to represent the file's data?
David M.
I am getting the data from a connection and storing it in an instance of NSData.
Geoff Baum
RTFM. In particular, use the -writeToURL:options:error: method of NSData. http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html
David M.
Now if I use the writeToFile method, and specified a path to the app bundle, would that save the file there where I could access it later? I need to retain the file, even if the internet connection is not present.
Geoff Baum
Scratch that. I am an idiot. That will work. Thanks!
Geoff Baum