Jayasabeen
Jayasabeen

Reputation: 136

Browse for sandbox files iOS device

I am new to iOS sandbox. I am trying to list out all files within the device sandbox. I am not able to succeed it

My Code is

- (void)viewDidLoad {
    NSLog(@"Files .. %@",[self showFiles]);
}


- (NSMutableArray *)showFiles
{
    NSError *err        = nil;
    NSArray *myPathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *myPath    = [myPathList  objectAtIndex:0];
    NSArray *dirContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:myPath error:&err];
    if(err) NSLog(@"showFiles() - ERROR: %@",[err localizedDescription]);
    NSMutableArray *filePaths  = nil;

    int count = (int)[dirContent count];
    for(int i=0; i<count; i++)
    {
        [filePaths addObject:[dirContent objectAtIndex:i]];
    }
    return filePaths;
}

Thanks in advance

Upvotes: 1

Views: 150

Answers (1)

trojanfoe
trojanfoe

Reputation: 122391

OK, you are getting nil as you are passing back an unallocated array object:

NSMutableArray *filePaths  = nil;

You want:

NSMutableArray *filePaths  = [NSMutableArray new];

However given you are copying the filenames into this array from dirContent, you may just as well return dirContent directly:

- (NSMutableArray *)showFiles
{
    NSError *err        = nil;
    NSArray *myPathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *myPath    = [myPathList  objectAtIndex:0];
    NSArray *dirContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:myPath error:&err];
    if (!dirContent)
        NSLog(@"showFiles() - ERROR: %@",[err localizedDescription]);
    return dirContent;
}

One further note: You are getting the contents of the Caches folder, which will be empty by default, as far as I know.

Upvotes: 3

Related Questions