Last active
January 5, 2021 16:30
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (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