- (NSString *) UTIforFileExtension:(NSString *) extension {
NSString * UTIString = (NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
(CFStringRef)extension,
NULL);
return [UTIString autorelease];
}
- (BOOL) setMyselfAsDefaultApplicationForFileExtension:(NSString *) fileExtension {
OSStatus returnStatus = LSSetDefaultRoleHandlerForContentType (
(CFStringRef) [self 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;
}
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFile</key>
<string>Icon file for associated file</string>
<key>CFBundleTypeName</key>
<string>My file format</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string> <!-- The value can be Editor, Viewer, Shell, or None. This key is required. -->
<key>LSItemContentTypes</key>
<array>
<string>UTI of the file</string> <!-- Existing UTI or create a UTI for your new file type -->
</array>
<key>LSHandlerRank</key>
<string>Owner</string>
</dict>
</array>