0

I click the button and check if the (.png) or another data is exists in NSDocumentDirectory. if exists push the view controller or not.

I tried this,

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"u0-1b.png"];

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];

    UIViewController *controller = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"SavedViewController"];

    [self.navigationController pushViewController:controller animated:YES];
}
else
    NSLog(@"file doesnt exist");

Here I given the full image name (u1-1b.png) it's woking but I want to check the data or extension (.png, or ).

iPatel
  • 46,010
  • 16
  • 115
  • 137
user3069029
  • 211
  • 2
  • 10

2 Answers2

3

You can manage it by using following code

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
NSLog(@"files array %@", filePathsArray);

if(filePathsArray.count > 0)
{
     // data is exist;
}
else
{
  // data does not exist;
}

You can get the path for each object in filePathsArray by using the below code

for (int i = 0 ; i < filePathsArray.count; i++)
{
   NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:i]];
}

If you want to get extension of any file then @Basheer_CAD's Like is helpful for you.

iPatel
  • 46,010
  • 16
  • 115
  • 137
3

This method return to you all the files paths and extensions in NSDictionary in your document dir, make use of it. The directory parameter, is used if you want to search for subfolders, example: if you have files in folder mmm pass @"/mmm", otherwise pass nil

 - (NSDictionary*)listOfFilesAtDirectory:(NSString*)directory
    {
        NSMutableDictionary *filesAtDirectory = [[NSMutableDictionary alloc] init];
        NSString *documentsDir = [self documentsDirectory];
        NSString *givenDirectory = [documentsDir stringByAppendingString:directory];
        NSError *searchErr = nil;

        NSArray *arrFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:givenDirectory
                                                                                error:&searchErr];
        if(!searchErr)
        {
            for(NSString *strFileNameAndExtension in arrFiles)
            {
                NSString *strFilePath = [givenDirectory stringByAppendingPathComponent:strFileNameAndExtension];
                [filesAtDirectory setObject:strFileNameAndExtension forKey:strFilePath];
            }
        }
        else
        {
            NSLog(@"Error reading files in this directory: %@, \n Error description:%@", directory, searchErr.description);
        }

        return filesAtDirectory;
    }

Just in case this is documentsDirectory method

- (NSString*)documentsDirectory
{
    NSString *strRootPath  = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    return strRootPath;
}
Basheer_CAD
  • 4,908
  • 24
  • 36