handleGetURLEvent不会被调用

tam*_*gal 6 events cocoa event-handling url-scheme

我正在为我的某个应用程序实现自定义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>
Run Code Online (Sandbox Code Playgroud)

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

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

但是当我点击带有URL的链接时,不会调用该方法."myscheme://测试"

- (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);
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Rob*_*ger 6

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

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

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

/System/Library/Frameworks/ApplicationServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user
Run Code Online (Sandbox Code Playgroud)