Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect file creation date on iPhone OS?

I was planning on writing some code whose logic was based upon testing the creation date of a particular file in my app's Documents folder. Turns out, when I call -[NSFileManager attributesOfItemAtPath:error:], NSFileCreationDate isn't one of the provided attributes.

Is there no way to discover a file's creation date?

Thanks.

like image 825
Greg Maletic Avatar asked Mar 17 '10 05:03

Greg Maletic


2 Answers

The fileCreationDate is indeed part of the dictionary. Here's a method that gets passed a file URI and grabs some of the attributes from the file:

- (NSDictionary *) attributesForFile:(NSURL *)anURI {

// note: singleton is not thread-safe
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *aPath = [anURI path];

if (![fileManager fileExistsAtPath:aPath]) return nil;

NSError *attributesRetrievalError = nil;
NSDictionary *attributes = [fileManager attributesOfItemAtPath:aPath
                           error:&attributesRetrievalError];

if (!attributes) {
   NSLog(@"Error for file at %@: %@", aPath, attributesRetrievalError);
   return nil;
}

NSMutableDictionary *returnedDictionary = 
   [NSMutableDictionary dictionaryWithObjectsAndKeys:
        [attributes fileType], @"fileType",
        [attributes fileModificationDate], @"fileModificationDate",
        [attributes fileCreationDate], @"fileCreationDate",
        [NSNumber numberWithUnsignedLongLong:[attributes fileSize]], @"fileSize",
    nil];

return returnedDictionary;
}
like image 75
memmons Avatar answered Oct 27 '22 02:10

memmons


According to Apple's reference, NSFileCreationDate is available in 2.0+:

NSFileCreationDate The key in a file attribute dictionary whose value indicates the file's creation date.

The corresponding value is an NSDate object.

Available in iPhone OS 2.0 and later.

Declared in NSFileManager.h.

like image 44
Shaggy Frog Avatar answered Oct 27 '22 00:10

Shaggy Frog