user2541457
user2541457

Reputation: 81

View the contents of Custom Directory in iOS

I am creating a directory inside my application.Is there a code to view the contents of the directory in xcode. For example in android you can create a custom directory and view its contents using a file manager application. Can the similar procedure be done in apple? Here is the code which i use to create a directory?

bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
    fileManager = [NSFileManager defaultManager];
    myImageDirectory = [fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
    if ([myImageDirectory count] == 1){
        NSLog(@"myImageDirectoryIs already present directory is already present");
    }else{
        directoryPath = [[myImageDirectory objectAtIndex:0] URLByAppendingPathComponent:bundleIdentifier];
        NSLog(@"myImageDirectory directory name = %@",[directoryPath absoluteString]);
        NSError *theError = nil;
        if (![fileManager createDirectoryAtURL:directoryPath withIntermediateDirectories:NO attributes:nil error:&theError]){
            NSLog(@"didnt write image data");
        }else{
           imagePath = [[directoryPath absoluteString] stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@_%@_%@_image.jpg",dIdNo,iIdNo,[self currentDateandTime]]];
           [imageData writeToFile:imagePath atomically:YES];
        }
    }

Upvotes: 0

Views: 167

Answers (4)

Jesse
Jesse

Reputation: 1697

If your app is running in the simulator you'll need to use Finder. Go to the following directory:

/Users/<username>/Library/Application Support/iPhone Simulator/<iOS version>/Applications/<uuid>/Library/Application Support

If your app is running on the device you can use Xcode:

  • Connect the device
  • Choose menu option Window -> Organizer
  • Go to the Devices Tab
  • Click Applications under the device menu on the left
  • Pick your Application
  • The directory contents will be listed and you can optionally download everything.

Upvotes: 1

Fernando Cervantes
Fernando Cervantes

Reputation: 2962

Method

-(NSArray *) getObjectsInDirectory:(NSString *)directory {
    NSFileManager * fm = [NSFileManager defaultManager];
    NSError * error = nil;

    NSArray * result = [fm contentsOfDirectoryAtPath:directory error:&error];

    return result;
}

How-To

NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray * files = [self getobjectsInDirectory:documentsPath];

This code is used to get an array of the files within the documents directory.

Viewing the Files

NSLog(@"%@", files);

Check this class out, it allows you to simplify so many things in iOS: Atomic Class

Upvotes: 0

Shubham
Shubham

Reputation: 570

you can get the contents of the directory(MYNewFolder) from below code:

NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   
 NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"MYNewFolder"];
NSArray *filePathsArray = [[NSFileManager defaultManager]
                           subpathsOfDirectoryAtPath:stringPath
                           error:nil];
for ( NSString *apath in filePathsArray )
{
    NSLog(@"inside the file path array=%d",apath);
}

Upvotes: 0

Muruganandham K
Muruganandham K

Reputation: 5331

try this... to create a directory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"];

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder

to retrieve contents from directory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *customDirectory = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"];
NSError * error;
NSArray *directoryContents =  [[NSFileManager defaultManager]
                                       contentsOfDirectoryAtPath:customDirectory error:&error];
        for(NSString *strFile in directoryContents)
        {
            NSString *strVideoPath = [NSString stringWithFormat:@"%@/%@",customDirectory,strFile];

        if([[strVideoPath pathExtension] isEqualToString:@"mp4"] || [[strVideoPath pathExtension] isEqualToString:@"mov"])
        {

            [urlArray addObject:strVideoPath];
        }
    }

Upvotes: 0

Related Questions