9

I'm building an app that allows users to drop videos onto it. Given a list of dropped NSURL*s how do I make sure each one conforms to the public.movie UTI type?

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

Thanks in advance!

simon.d
  • 2,471
  • 3
  • 33
  • 51

3 Answers3

21

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.)

Wevah
  • 28,182
  • 7
  • 83
  • 72
1

Swift version:

do {
    var value: AnyObject?
    try url.getResourceValue(&value, forKey:NSURLTypeIdentifierKey)
    if let type = value as? String {
        if UTTypeConformsTo(type, kUTTypeMovie) {
        ...
        }
    }
}
catch {
}
david72
  • 7,151
  • 3
  • 37
  • 59
0

Swift 5 version:

if let resourceValues = try? localUrl.resourceValues(forKeys: [URLResourceKey.typeIdentifierKey]) {
    if let typeId = resourceValues.typeIdentifier {
        if UTTypeConformsTo(type, kUTTypeMovie) {
            ...
        }
    }
}
Ryan H
  • 1,656
  • 14
  • 15