I have the list of the applications for given file extension (using LSCopyApplicationURLsForURL). I want to change the default file association from code upon selecting one
Here’s a slightly modified and ARC-compliant version of Guillaume’s solution:
#import
@interface LaunchServicesWrapper : NSObject
+ (BOOL)setMyselfAsDefaultApplicationForFileExtension:
(NSString *)fileExtension;
@end
#import
#import "LaunchServicesWrapper.h"
@implementation LaunchServicesWrapper
+ (NSString *)UTIforFileExtension:(NSString *)extension
{
return (NSString *)CFBridgingRelease(
UTTypeCreatePreferredIdentifierForTag(
kUTTagClassFilenameExtension, (__bridge CFStringRef)extension,
NULL
)
);
}
+ (BOOL)setMyselfAsDefaultApplicationForFileExtension:
(NSString *)fileExtension
{
return LSSetDefaultRoleHandlerForContentType(
(__bridge CFStringRef) [LaunchServicesWrapper
UTIforFileExtension:fileExtension], kLSRolesAll,
(__bridge CFStringRef) [[NSBundle mainBundle]
bundleIdentifier]
);
}
@end