Search code examples
ipadios5nsfilemanager

How to access and display documents from iPad3 programatically?


I am implementing an application that allows users to add and share different documents. I am done with adding documents by enabling "Application supports iTunes file sharing" in the plist. So user can add his/her documents directly to the application with the help of iTunes. Now my problem is I need access and display all documents under my application in a table view with their title. Based on the user selection I need to display it in a pdf or any other format.

How can I access all the documents under my application?

Also is there any other way to dump documents in to my application except using iTues? Please suggest a better option.


Solution

  • I found a solution for this. By using the following code snippet we can easily access all the files.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
    
    for (NSString *s in fileList)
    {
    
        if ([s hasSuffix:@".pdf"])
        {
            //do stuff
        }
        else
        {
           // do stuff
        }
    
    }