Created
January 4, 2011 19:45
-
-
Save laullon/765282 to your computer and use it in GitHub Desktop.
Get the mimetype for a extension. (untested)
This file contains 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*)mimeTypeForExtension:(NSString*)ext | |
{ | |
NSAssert( ext, @"Extension cannot be nil" ); | |
NSString* mimeType = nil; | |
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, | |
(CFStringRef)ext, NULL); | |
if( !UTI ) return nil; | |
CFStringRef registeredType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType); | |
if( !registeredType ) // check for edge case | |
{ | |
if( [ext isEqualToString:@"m4v"] ) | |
mimeType = @"video/x-m4v"; | |
else if( [ext isEqualToString:@"m4p"] ) | |
mimeType = @"audio/x-m4p"; | |
// handle anything else here that you know is not registered | |
} else { | |
mimeType = NSMakeCollectable(registeredType); | |
} | |
CFRelease(UTI); | |
return mimeType; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment