【问题标题】:Document Type Description or 'Kind' Description文件类型描述或“种类”描述
【发布时间】:2011-07-24 04:28:51
【问题描述】:

给定一种文件类型(例如,DOC 或 PDF),如何获得类似于 Finder 的“种类”的描述?例如,在 Finder 下,DOC Kind 为 'Word 97 Format (.doc)',PDF Kind 为 'Portable Document Format (PDF)。

根据About Document Interaction,“iOS 提供了系统范围的文件类型关联注册表”。但我不相信我提取了正确的信息(我什至不确定我使用的是上述注册表)。

我正在使用下面的代码,它为 DOC 类型返回“com.microsoft.word.doc”。 RTF 是“public.rtf”,PDF 是“com.adobe.pdf”。更奇怪的是一个链接(webloc):'dyn.age81s3pcrv10g'。这些描述都不是用户友好的。

NSURL* url = [NSURL fileURLWithPath:fullPathName];
if(url == nil)
  return [extension uppercaseString];    // i.e., PDF

UIDocumentInteractionController* doc = [UIDocumentInteractionController interactionControllerWithURL: url];     
if(doc == nil)
  return [extension uppercaseString];    // i.e., PDF

return doc.UTI;

【问题讨论】:

    标签: iphone file


    【解决方案1】:

    这是获取描述的代码。例如,PDF 文件检索“可移植文档格式 (PDF)”。

    要在 iPhone 上使用代码,请添加 MobileCoreServices 框架和#import <MobileCoreServices/MobileCoreServices.h>。为清楚起见,省略了错误检查和 ASSERT。

    NSString* extension = [filename pathExtension];
    NSString* hint = @"public.data";
    
    CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)extension, (CFStringRef)hint);
    [(NSString *)uti autorelease];
    
    CFStringRef mime = UTTypeCopyPreferredTagWithClass (uti, kUTTagClassMIMEType);
    [(NSString *)mime autorelease];
    
    CFStringRef description = UTTypeCopyDescription(uti);
    [(NSString*)description autorelease];
    
    NSLog(@"%@: %@", filename, description);
    

    ============================================

    堆栈溢出: Where's the iPhone MIME type database?

    苹果:Adopting Uniform Type Identifiers

    【讨论】:

    • 值得一提:hint = @"public.data"; 指定需要文件的 UTI。文件夹也有 UTI 信息(请参阅 Apple 的采用统一类型标识符)。
    猜你喜欢
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多