Question

I am working with NSFileManager and I am able to get the file's last update, creation date, ... but I am not able to get when was the last time the file was opened. Is there any way to get this information?

And other question, I get the MIME by the pathExtension property, but this way I get for example .pdf, .doc, ... instead of application/pdf, application/doc, text/txt, audio/vmw, ...is any way to get this information automatically?

Thanks a lot!

Was it helpful?

Solution 2

Use Spotlight's MDItem API to get all the info

NSString *path = @"/Users/dominik/Downloads/Screen Shot 2013-11-28 at 13.26.04.png";
MDItemRef item = MDItemCreate(NULL, (CFStringRef)path);
NSArray *attributes = (NSArray*)CFBridgingRelease(MDItemCopyAttributeNames(item));
NSDate *date = (NSDate*)CFBridgingRelease(MDItemCopyAttribute(item, kMDItemLastUsedDate));
CFRelease(item);
NSLog(@"%@",attributes);
NSLog(@"%@",date);

OTHER TIPS

I think NSURL's NSURLContentAccessDateKey may be close to what you are after and may help depending on what you are actually doing..

But it is accessed not opened.

Which means for example:

if you open the file it is accessed.

If you quicklook the file it is accessed.

NSURLContentAccessDateKey The time at which the resource was most recently accessed, returned as an NSDate object if the volume supports access dates, or nil if access dates are unsupported (read-only). Available in OS X v10.6 and later. Declared in NSURL.h.

Example:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

    [self lastAcccess:@"/Users/UserName/Pictures/screenshots/text.png"];
}



- (void) lastAcccess: (NSString *) the_path  {

    NSURL *theUrl = [NSURL  fileURLWithPath:the_path];
    NSError * error;

    NSDate  *theDate;
    [theUrl getResourceValue:&theDate forKey:NSURLContentAccessDateKey error:&error];

     NSLog(@" theDate %@", theDate);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top