Check NSURL for UTI / file type

I am creating an application that allows users to shoot videos on it. Given the list of fallen NSURL * s, how can I make sure that each one matches the public.movie UTI type?

If I had an NSOpenPanel , I would just use openPanel.allowedFileTypes = @[@"public.movie"]; and Cocoa took care of this for me.

Thanks in advance!

+7
source share
2 answers

This should work:

 NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; for (NSURL *url in urls) { NSString *type; NSError *error; if ([url getResourceValue:&type forKey:NSURLTypeIdentifierKey error:&error]) { if ([workspace type:type conformsToType:@"public.movie"]) { // the URL points to a movie; do stuff here } } else { // handle error } } 

(You can also use UTTypeConformsTo() instead of the NSWorkspace method.)

+17
source

Quick version:

 do { var value: AnyObject? try url.getResourceValue(&value, forKey:NSURLTypeIdentifierKey) if let type = value as? String { if UTTypeConformsTo(type, kUTTypeMovie) { ... } } } catch { } 
0
source

All Articles