I have a program for the iPhone that is supposed to be doing intelligent things (picking out appropriate icons for file types) given a list of filenames. I'm looking for the iPhone take on something like /etc/mime.types or something similar- an API call is what I'm assuming would be available for the phone. Does this exist?
Asked
Active
Viewed 1.2k times
4 Answers
49
If it did, your app surely wouldn't have permissions to even read it directly. What are you trying to do?
EDIT
This is a function I wrote a while ago. I wrote it for the Mac, but it looks like the same functions exist on the iPhone. Basically, you give it a filename, and it uses the path extension to return the file's MIME type:
#import <MobileCoreServices/MobileCoreServices.h>
...
- (NSString*) fileMIMEType:(NSString*) file {
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[file pathExtension], NULL);
CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);
CFRelease(UTI);
return [(NSString *)MIMEType autorelease];
}

Daniel Magnusson
- 9,541
- 2
- 38
- 43

Dave DeLong
- 242,470
- 58
- 448
- 498
-
8`Does not work on iPhone` - my bad. You need to include the MobileCoreServices framework, and `#import
` – jww Apr 02 '11 at 07:24 -
I'm having a problem with this code with a pdf file on one iPad device (iPad 1st gen / 5.1.1), but not another (iPad 3 / 5.1.1) or in Simulator (5.1). The pathExtension is pdf, and UTI comes back OK as com.adobe.pdf, but the MIMEType returns nil. Is there anything that might cause this? Can a MIMEType registry get corrupted on a device? – Ian Kershaw Jun 11 '12 at 15:04
-
1@slf yes, that's fundamentally equivalent to what's in the answer. – Dave DeLong Feb 07 '14 at 21:39
4
The following function will return the mime-type for a given file extension in Swift 2
import MobileCoreServices
func mimeTypeFromFileExtension(fileExtension: String) -> String? {
guard let uti: CFString = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension as NSString, nil)?.takeRetainedValue() else {
return nil
}
guard let mimeType: CFString = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() else {
return nil
}
return mimeType as String
}

dreamlab
- 3,321
- 20
- 23
1
In obj-C, warning to memory leaks when using C.
- (NSString *)guessMIMETypeFromFileName: (NSString *)fileName {
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[fileName pathExtension], NULL);
CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
CFRelease(UTI);
if (!MIMEType) {
return @"application/octet-stream";
}
NSString *dest = [NSString stringWithString:(__bridge NSString *)(MIMEType)];
CFRelease(MIMEType);
return dest;
}

Emmanuel Crombez
- 295
- 2
- 4
0
Updating the great and accepted answer to Swift 5.3, as an URL extension
extension URL {
var mime: String {
guard
let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as CFString, nil)
else { return "" }
let mime = uti.takeRetainedValue() as String
uti.release()
return mime
}
}

vicegax
- 4,709
- 28
- 37