Created
December 28, 2011 10:40
-
-
Save ddeville/1527517 to your computer and use it in GitHub Desktop.
MIME type to UTI and back again in Cocoa
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
#if TARGET_OS_IPHONE | |
#import <MobileCoreServices/MobileCoreServices.h> | |
#else | |
#import <CoreServices/CoreServices.h> | |
#endif | |
/* | |
MIME type to UTI | |
*/ | |
NSURLResponse *response = ... // assume a URL response from somewhere else. | |
NSString *responseMIMEType = [response MIMEType]; | |
CFStringRef MIMEType = (__bridge CFStringRef)[response MIMEType]; | |
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, MIMEType, NULL); | |
NSString *UTIString = (__bridge_transfer NSString *)UTI; | |
/* | |
UTI to MIME type | |
*/ | |
NSString *filePath = ... // assume the path to a file from somewhere else. | |
CFStringRef fileExtension = (__bridge CFStringRef)[filePath pathExtension]; | |
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL); | |
CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType); | |
CFRelease(UTI); | |
NSString *MIMETypeString = (__bridge_transfer NSString *)MIMEType; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Swift: