Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
70 views
in Technique[技术] by (71.8m points)

iOS: How do you find the creation date of a file?

I'm trying to find the creation date (NOT modification date) of a file.

Creation date doesn't appear to be in the attributes of a file, though modified date is.

I'm using this code..

NSFileManager* fm = [NSFileManager defaultManager];

NSString* path = [PathHelpers pathInDocumentsFolderWithFilename:FILE_NAME];
NSDictionary* attrs = [fm attributesOfItemAtPath:path error:nil];

if (attrs != nil) {
    return (NSDate*)[attrs objectForKey: NSFileCreationDate];
} else {
    return nil;
}

This always returns nil. Typing 'po attrs' into the debugger to get the list of key/value pairs in the NSDictionary returns the following..

NSFileGroupOwnerAccountID = 20;
NSFileGroupOwnerAccountName = staff;
NSFileModificationDate = 2010-01-21 11:47:55 +0000;
NSFileOwnerAccountID = 501;
NSFileOwnerAccountName = ben;
NSFilePosixPermissions = 420;
NSFileReferenceCount = 1;
NSFileSize = 338;
NSFileSystemFileNumber = 2234;
NSFileSystemNumber = 42553324;
NSFileType = NSFileTypeRegular;

No creation date.. bah..

Anyone know another way of getting the creation date or does it just not exist in iOS?

question from:https://stackoverflow.com/questions/2108953/ios-how-do-you-find-the-creation-date-of-a-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This code actually returns the good creation date to me:

NSFileManager* fm = [NSFileManager defaultManager];
NSDictionary* attrs = [fm attributesOfItemAtPath:path error:nil];

if (attrs != nil) {
    NSDate *date = (NSDate*)[attrs objectForKey: NSFileCreationDate];
    NSLog(@"Date Created: %@", [date description]);
} 
else {
    NSLog(@"Not found");
}

Are you creating the file inside the App? Maybe that's where the problem is.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...