-1

Given a file type (eg, DOC or PDF), how does one get a description similar to Finder's 'Kind'? For example, under Finder, DOC Kind is 'Word 97 Format (.doc)', and PDF Kind is 'Portable Document Format (PDF).

According to About Document Interaction, " iOS provides a systemwide registry of file type associations". But I don't believe I am pulling out the correct information (I'm not even really sure I'm using the aforementioned registry).

I'm using the code below, and it is returning 'com.microsoft.word.doc' for a DOC's kind. RTF is 'public.rtf', and PDF is 'com.adobe.pdf'. More bizarre is a link (webloc): 'dyn.age81s3pcrv10g'. None of the descriptions are user friendly.

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;
jww
  • 97,681
  • 90
  • 411
  • 885

1 Answers1

0

Here's the code to get the description. For example, a PDF files retrieves 'Portable Document Format (PDF)'.

To use the code on the iPhone, add the MobileCoreServices framework and #import <MobileCoreServices/MobileCoreServices.h>. Error checking and ASSERTs have been omitted for clarity.

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

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

Stack Overflow: Where's the iPhone MIME type database?

Apple: Adopting Uniform Type Identifiers

Community
  • 1
  • 1
jww
  • 97,681
  • 90
  • 411
  • 885
  • Worth mentioning: `hint = @"public.data";` specifies that a file's UTI is desired. Folders also have UTI information (see Apple's Adopting Uniform Type Identifiers). – jww Apr 02 '11 at 22:20