Here is a snippet of code for a very related task: set yourself as the default application for a given file extension:
#import <ApplicationServices/ApplicationServices.h>
#import "LaunchServicesWrapper.h"
@implementation LaunchServicesWrapper
+ (NSString *) UTIforFileExtension:(NSString *) extension {
NSString * UTIString = (NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
(CFStringRef)extension,
NULL);
return [UTIString autorelease];
}
+ (BOOL) setMyselfAsDefaultApplicationForFileExtension:(NSString *) fileExtension {
OSStatus returnStatus = LSSetDefaultRoleHandlerForContentType (
(CFStringRef) [LaunchServicesWrapper UTIforFileExtension:fileExtension],
kLSRolesAll,
(CFStringRef) [[NSBundle mainBundle] bundleIdentifier]
);
if (returnStatus != 0) {
NSLog(@"Got an error when setting default application - %d", returnStatus);
// Please see the documentation or LSInfo.h
return NO;
}
return YES;
}
@end
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…