Skip to content

Instantly share code, notes, and snippets.

@laullon
Created January 4, 2011 19:45
Show Gist options
  • Save laullon/765282 to your computer and use it in GitHub Desktop.
Save laullon/765282 to your computer and use it in GitHub Desktop.
Get the mimetype for a extension. (untested)
-(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