retreiving files from application directory

Discussion in 'iOS Programming' started by newlearner, Sep 23, 2009.

  1. newlearner macrumors member

    newlearner

    Joined:
    Jul 30, 2009
    Location:
    india
    #1
    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:

    Code:
    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
     
  2. Troglodyte macrumors member

    Joined:
    Jul 2, 2009
  3. drivefast macrumors regular

    Joined:
    Mar 13, 2008
    #3
    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.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Look at the NSFileManager docs for ways to manipulate the file system from Cocoa.

    Code:
    NSArray*	fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:inPath error:nil];
    
     
  5. newlearner thread starter macrumors member

    newlearner

    Joined:
    Jul 30, 2009
    Location:
    india
    #5
    thank you all,

    i am able to retrieve the file names using the following code:

    Code:
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
    	
    
     
  6. drivefast macrumors regular

    Joined:
    Mar 13, 2008
    #6
    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.
     
  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    @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.
     
  8. idelovski macrumors regular

    Joined:
    Sep 11, 2008
  9. electroshock macrumors 6502a

    electroshock

    Joined:
    Sep 7, 2009
    #9
    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
     
  10. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #10
    @electroshock and @drivefast, you didn't answer my question

    Or to put it more simply, if that code is wrong what code is right?
     
  11. drivefast macrumors regular

    Joined:
    Mar 13, 2008
    #11
    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:
    Code:
    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.
     
  12. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #12
    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/libr...hs.html#//apple_ref/doc/uid/20000152-BBCBIGHH

    See also: http://developer.apple.com/mac/libr...Directories.html#//apple_ref/doc/uid/20001279
     
  13. drivefast macrumors regular

    Joined:
    Mar 13, 2008
    #13
    myeah... i'll admit it since it's official recommendation, but i dont think i like their implementation :eek: i still dont feel like i'm in supposed to go back and modify all my code base to use the NSDocumentsDirectory method.
     

Share This Page