Question

How to retrieve all app bundle IDs which can open file at given URL? Like if file at given URL is .xml I want to get array of all app bundle IDs which can open .xml.

Was it helpful?

Solution

You can use Launch Services' LSCopyAllRoleHandlersForContentType() to get an array of bundle identifiers of capable applications.

Code might look something like the following:

NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"sample"
                                         withExtension:@"xml"];

NSString *utiType = nil;
NSError *error = nil;

BOOL success = [fileURL getResourceValue:&utiType
                         forKey:NSURLTypeIdentifierKey error:&error];

if (!success) {
    NSLog(@"getResourceValue:forKey:error: returned error == %@", error);
}

NSArray *bundleIdentifiers = [(NSArray *)LSCopyAllRoleHandlersForContentType(
                         (CFStringRef)utiType, kLSRolesAll) autorelease];

NSLog(@"bundleIdentifiers == %@", bundleIdentifiers);

In most recent versions of OS X, Launch Services is part of the CoreServices.framework umbrella framework. You may need to #import it in the class you want to call the LS* functions in, as well as add it to the Link Binary With Libraries Build Phase of your target. (It seemed to work OK here without linking against it in the OS X 10.8 SDK).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top