Skip to content

Instantly share code, notes, and snippets.

@dduan
Created July 7, 2014 02:34
Show Gist options
  • Save dduan/2ecc257ab908aff24530 to your computer and use it in GitHub Desktop.
Save dduan/2ecc257ab908aff24530 to your computer and use it in GitHub Desktop.
Guessing MIME type from a file name in Objective-C on iOS
- (NSString *)guessMIMETypeFromFileName: (NSString *)fileName {
// Borrowed from http://stackoverflow.com/questions/2439020/wheres-the-iphone-mime-type-database
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[fileName pathExtension], NULL);
CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
CFRelease(UTI);
if (!MIMEType) {
return @"application/octet-stream";
}
return (__bridge NSString *)(MIMEType);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment