5

我有给定文件扩展名的应用程序列表(使用 LSCopyApplicationURLsForURL)。从上述调用中选择一个应用程序后,我想从代码中更改默认文件关联。有没有办法做到这一点?

4

3 回答 3

9

这是一个非常相关的任务的代码片段:将自己设置为给定文件扩展名的默认应用程序:

#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
于 2011-12-27T13:59:34.313 回答
1

这是 Guillaume 解决方案的略微修改且符合 ARC 的版本:

#import <Foundation/Foundation.h>

@interface LaunchServicesWrapper : NSObject

+ (BOOL)setMyselfAsDefaultApplicationForFileExtension:
  (NSString *)fileExtension;

@end


#import <ApplicationServices/ApplicationServices.h>
#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
于 2013-04-26T13:00:26.450 回答
-2
- (void) setApplication:(NSString *)applicationName forExtension:(NSString *)extension {

    NSArray *appPaths = [self getApplicationListForExtension:extension];

    for (NSString *appPath in appPaths) {
        if ([appPath rangeOfString:applicationName].location != NSNotFound) {
            NSArray *UTIs = (NSArray *)UTTypeCreateAllIdentifiersForTag(kUTTagClassFilenameExtension,
                                                                        (CFStringRef)extension,
                                                                        nil);
            for (NSString *UTI in UTIs) {
                LSSetDefaultRoleHandlerForContentType((CFStringRef)UTI,
                                                      kLSRolesEditor,
                                                      (CFStringRef)[[NSBundle bundleWithPath:appPath] bundleIdentifier]);
            }

            [UTIs release];

            break;
        }
    }
}
于 2010-07-27T14:31:04.847 回答