Skip to content

Instantly share code, notes, and snippets.

@itsthejb
Created January 28, 2014 23:41
Show Gist options
  • Save itsthejb/8678922 to your computer and use it in GitHub Desktop.
Save itsthejb/8678922 to your computer and use it in GitHub Desktop.
Obj-C wrapper and categories for NSString and NSURL for Cocoa UTTypes
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSUInteger, NSUTType) {
NSUTTypeImage,
NSUTTypeMovie,
NSUTTypeAudio,
NSUTTypeUnknown
};
@interface NSURL (UTType)
- (NSUTType) UTType;
@end
@interface NSString (UTType)
- (NSUTType) UTType;
@end
#import "Foundation+UTType.h"
@implementation NSURL (UTType)
- (NSUTType) UTType { return self.absoluteString.UTType; }
@end
@implementation NSString (UTType)
- (NSUTType) UTType {
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
(__bridge CFStringRef)(self.pathExtension),
NULL);
static NSDictionary *map = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
map = @{
(id) kUTTypeImage : @(NSUTTypeImage),
(id) kUTTypeMovie : @(NSUTTypeMovie),
(id) kUTTypeAudio : @(NSUTTypeAudio)
};
});
NSUTType ret = NSUTTypeUnknown;
for (NSString *type in map.allKeys) {
if (UTTypeConformsTo(UTI, (__bridge CFStringRef)(type))) {
ret = [map[type] integerValue];
}
}
CFRelease(UTI);
return ret;
}
@end
@itsthejb
Copy link
Author

TODO: expand to more types/subtypes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment