View Full Version : retreiving files from application directory
newlearner
Sep 23, 2009, 09:03 AM
hello all,
i want to retreive the files which have been written into the applications documents directory. I have used this code to write the files:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *FilePath = [documentsDirectory stringByAppendingPathComponent: FileName];
NSURL *url = [NSURL fileURLWithPath:FilePath];
Now i want to retreive all the written files from Documents and display them into a table. So, how to get back all the files in an array?
thanks
Troglodyte
Sep 23, 2009, 10:25 AM
The code you posted doesn't write anything.
drivefast
Sep 23, 2009, 11:31 AM
the first line in your code retrieves all the file names, including paths, in an array of strings. you will normally to iterate thru this array to display the file names, although in your case you may not have to. i would suggest following the recipes in "Table View programming guide for the iPhone OS" (part of the iphone os sdk) - they should be quite straightforward for what you need. you probably wont need to use anything else than the 1st line in your code.
PhoneyDeveloper
Sep 23, 2009, 12:49 PM
Look at the NSFileManager docs for ways to manipulate the file system from Cocoa.
NSArray* fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:inPath error:nil];
newlearner
Sep 24, 2009, 02:35 AM
thank you all,
i am able to retrieve the file names using the following code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
drivefast
Sep 24, 2009, 02:28 PM
it's ok, but it's fragile. reason is, your documents directory may not always be at index 0 in your paths array. you should iterate over your paths array until you find your documents directory, then go after the files in it. this way, you're covered.
PhoneyDeveloper
Sep 24, 2009, 04:09 PM
@drivefast, I don't think so. There has never been more than one Documents folder per app on the phone. What criteria do you use to tell if you've found your Documents folder?
I've never seen any other code suggested for this purpose.
idelovski
Sep 24, 2009, 05:45 PM
Or maybe using NSHomeDirectory() function as in this example (http://icodeblog.com/2009/09/09/code-snippet-quickly-find-the-documents-directory/)?
electroshock
Sep 24, 2009, 10:37 PM
@drivefast, I don't think so. There has never been more than one Documents folder per app on the phone.
Bad idea to hardcode that kind of assumption because if Apple changes that in the future, your app may break or have unexpected behavior. In about 20 years of Mac development, I've learned to try to never assume a particular implementation will always remain that way -- the hard way. :D
PhoneyDeveloper
Sep 24, 2009, 10:57 PM
@electroshock and @drivefast, you didn't answer my question
What criteria do you use to tell if you've found your Documents folder?
Or to put it more simply, if that code is wrong what code is right?
drivefast
Sep 26, 2009, 04:17 PM
i'm usually concatenating the response of NSHomeDirectory() with "/Documents", and in most cases with a filename, because i usually know what file i'm looking for. sort of like this:
NSString *myFileFQN = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), myFileName];
i write the files building the fqn's as such, and i read them back the same, so this sounds like safe enough to me.
i didnt said the OP's method method was wrong, i just said it seems fragile, and this is a personal opinion. if the result of a function that i call is an array of paths, this is a strong suggestion to me that i should go and look for what i need into what's returned, rather than assuming that the array only contains one element, or what i need will always be in the first position.
PhoneyDeveloper
Sep 26, 2009, 07:34 PM
Hardcoding the Documents folder name is fragile. On iPhone OS that folder isn't displayed to the user so the name could be anything that Apple wants. The OP's code will always work if they change the name. Not yours.
The reason that the code returns an array is because this function is shared with MacOS X and can be used to return a bunch of different system folders. On iPhone OS developers don't have access to most of those folders, and they may not exist.
Really, apple should provide an iPhone-only single API that returns the user's Documents folder.
You might want to look at this apple page where they say that doing it the way you suggest is not preferred but doing it the way the OP does is preferred, under User Directories: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Strings/Articles/ManipulatingPaths.html#//apple_ref/doc/uid/20000152-BBCBIGHH
See also: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/LowLevelFileMgmt/Articles/StandardDirectories.html#//apple_ref/doc/uid/20001279
drivefast
Sep 26, 2009, 11:41 PM
myeah... i'll admit it since it's official recommendation, but i dont think i like their implementation :o i still dont feel like i'm in supposed to go back and modify all my code base to use the NSDocumentsDirectory method.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.