Search code examples
iphoneiosobjective-cnsfilemanager

Check if file is updated


I have iOS application. There some files in the documents directory. I need to check if these files were edited or replaced. What is the proper way to do it? Shall i some hash of the file or last edited time(how to get this time in iOS) or there is some better way to implement this?


Solution

  • you can use this code to get the file modification date:

    NSFileManager* fileManager = [NSFileManager defaultManager];
    NSDictionary* attributes = [fileManager attributesOfItemAtPath:path error:nil];
    
    if (attributes != nil) {
        NSDate *date = (NSDate*)[attributes objectForKey:NSFileModificationDate];
        NSLog(@"Last modification date: %@", [date description]);
    } else {
        NSLog(@"Not found");
    }
    

    here you can find the documentation of the attributesOfItemAtPath:error: function, and here the list of file properties you can get