Getting metadata by NSFileManager

给你一囗甜甜゛ 提交于 2019-12-11 05:14:28

问题


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!


回答1:


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);



回答2:


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);

}


来源:https://stackoverflow.com/questions/20288761/getting-metadata-by-nsfilemanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!