6

我正在为我的一个应用程序实施自定义 URL 方案,但无法使其正常工作。

我将这些行添加到我的 Info.plist 中:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>MyApp URL</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>myscheme</string>
        </array>
    </dict>
</array>

在我的应用程序委托中,我在 ApplicationDidFinishedLaunching 中安装了事件处理程序:

NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];

但是当我单击带有 URL 的链接时,不会调用该方法,例如。“我的方案://测试”

- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
           withReplyEvent:(NSAppleEventDescriptor *)replyEvent {

    // Extract the URL from the Apple event and handle it here.
    NSString* url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
    NSLog(@"%@", url);
}

我错过了什么?

4

4 回答 4

9

听起来您可能需要清理您的项目。有时,当 Xcode 构建应用程序时,启动服务数据库(处理 URL 关联)没有正确更新。清理项目应该完全删除构建的应用程序,因此下次构建项目时,它是从头开始创建的,在更新启动服务数据库的过程中。

您可能还想尝试将应用程序复制到该/Applications文件夹​​中,这将使 Launch Services 重新解析应用程序的Info.plist文件。

您可以通过在终端中运行以下命令来强制启动服务重建其数据库:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user
于 2012-04-18T12:31:48.887 回答
4

将事件处理程序代码移至init方法:

- (id) init
{   
    if ((self = [super init]))
    {
        NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
        [appleEventManager setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];

        // Add the following to set your app as the default for this scheme
        NSString * bundleID = [[NSBundle mainBundle] bundleIdentifier];
        LSSetDefaultHandlerForURLScheme((CFStringRef)@"myscheme", (CFStringRef)bundleID 
    }
return self;
}

注意: myscheme应该采用这样的形式x-com-companyname-appname,使其永远不会与任何其他方案发生冲突。

另请参阅:有关此主题的更多信息,请参阅如何将 Cocoa 应用程序设置为默认 Web 浏览器?

于 2012-02-15T12:38:51.817 回答
2

更新数据库 OS10.8 Mountain Lion

/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -f

-kill Reset the Launch Services database before doing anything else -seed If database isn't seeded, scan default locations for applications and libraries to register -lint Print information about plist errors while registering bundles -convert Register apps found in older LS database files -lazy n Sleep for n seconds before registering/scanning -r Recursive directory scan, do not recurse into packages or invisible directories -R Recursive directory scan, descending into packages and invisible directories

-f force-update registration even if mod date is unchanged

-u unregister instead of register -v Display progress information -dump Display full database contents after registration -h Display this help

于 2014-03-23T18:36:11.827 回答
1

显然在沙箱下您需要在 applicationWillFinishLaunching: 中注册,而不是 applicationDidFinishLaunching:

请参阅Apple 的文档

于 2015-03-28T21:04:59.007 回答