Skip to content

Instantly share code, notes, and snippets.

@rais38
Last active January 5, 2021 16:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rais38/5661946 to your computer and use it in GitHub Desktop.
Save rais38/5661946 to your computer and use it in GitHub Desktop.
On Mac OS X (LaunchServices and UTIs) to convert a file path to the file's MIME type using.
+ (NSString *)MIMETypeForFileAtPath:(NSString *)path defaultMIMEType:(NSString *)defaultType
{
NSString *result = defaultType;
NSString *extension = [path pathExtension];
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
(CFStringRef)extension,
NULL);
if (uti) {
CFStringRef cfMIMEType = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType);
if (cfMIMEType) {
result = [[(NSString *)cfMIMEType copy] autorelease];
CFRelease(cfMIMEType);
}
CFRelease(uti);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment