Find the Last Accessed Date of a File in Cocoa

The C way of doing it, using the stat system call will work in Objective-C.

e.g.

struct stat output;
int ret = stat(aFilePath, &output);
// error handling omitted for this example
struct timespec accessTime = output.st_atime;

You should get aFilePath by sending -fileSystemRepresentation to an NSString containing the path.

Another way you might get what you want is to construct an NSURL fore a file URL pointing to the file you want and using -resourceValuesForKeys:error: to get the NSURLContentAccessDate resource value.


Using NSMetadataQuery you can access spotlight metadata from your code. The last used date attribute of a file is tracked by spotlight and you can access that with this property: kMDItemLastUsedDate.