11

we know that we can get the file's MIME TYPE from the file's extension,but it's not exactly.for example we changed the file's extension and we will get the wrong mime type. and also we know the way that get the mime type by a file signature in C#,(using the urlmon.dll Using .NET, how can you find the mime type of a file based on the file signature not the extension ,my question is that how can we get the exact mime type in IOS,no matter the file's extension is changed by someone,we can getthe right mime type.

thank you for your attention~!

Community
  • 1
  • 1
HamasN
  • 524
  • 1
  • 5
  • 20

1 Answers1

27

Would you tell me how I get the right mime type exactly when I get a path of a file.

iOS uses the concept of Uniform Type Identifiers (UTI) to handle document types.

NSString *path; // contains the file path

// Get the UTI from the file's extension:

CFStringRef pathExtension = (__bridge_retained CFStringRef)[path pathExtension];
CFStringRef type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, NULL);
CFRelease(pathExtension);

// The UTI can be converted to a mime type:

NSString *mimeType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType);
if (type != NULL)
    CFRelease(type);

You should consider using UTIs for your purpose directly instead of converting them to the less powerful mime type.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • 4
    it won't give correct mimetype. It's not even checking actual file. It's just translating **extension to mimetype** – Johnnywho Jan 21 '13 at 13:30
  • 1
    @Johnnywho This answer is about getting the mime type **from a path** (as stated explicitly and was requested by the OP in a comment). – Nikolai Ruhe Jan 21 '13 at 13:40
  • 5
    But original question with its title regards the mime type **not based** on the extension. In that way, your answer is misleading and I wasted my time trying your code. You should state explicitly that the path must've an extension. None part of your answer is saying that. – Johnnywho Jan 22 '13 at 10:32
  • 7
    I know this is an old discussion, but I came across it just now through Google, and it was helpful. Note that you also need: `#import `, and to add the MobileCoreServices framework, to make it work. – Racing Tadpole Jun 07 '13 at 04:56
  • @NikolaiRuhe why not use `__bridge` instead of `__bridge_retained`? if you use `__bridge`, the `CFRelease(pathExtension)` is unnecessary. – oldman Jul 02 '14 at 09:30
  • @oldman That would mean that the temporary object returned from `pathExtension` would have no explicit strong reference but being used after. I'm not sure about the guarantees ARC gives when using a derived pointer (the `CFStringRef`) about the lifetime of the bridged object. – Nikolai Ruhe Jul 02 '14 at 09:37
  • @NikolaiRuhe you can use `UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge)[path pathExtension], NULL);` ^_^ – oldman Jul 03 '14 at 05:49
  • Anyone figured this out in Swift yet? – Chris Harrison Mar 20 '15 at 08:26
  • OP asked for a mime type NOT based on extension. – RedPoppy Dec 23 '17 at 10:40