handleGetURLEvent 不会被调用
我正在为我的一个应用程序实现自定义 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 的链接(例如)时,不会调用该方法。 “myscheme://test”
- (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);
}
我错过了什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来您可能需要清理您的项目。有时,当 Xcode 构建应用程序时,启动服务数据库(处理 URL 关联)不会正确更新。清理项目应该完全删除构建的应用程序,因此下次构建项目时,它会从头开始创建,并在此过程中更新启动服务数据库。
您可能还想尝试将应用复制到
/Applications
文件夹中,这应该会使 Launch Services 重新解析应用的Info.plist
文件。您可以通过在终端中运行以下命令来强制启动服务重建其数据库:
It sounds like you may need to clean your project. Sometimes the Launch Services database (which handles URL associations) is not updated correctly when Xcode builds an app. Cleaning the project should delete the built app entirely, so the next time you build the project it is created from scratch, in the process updating the Launch Services database.
You might also want to try copying the app into the
/Applications
folder, which should make Launch Services re-parse the app'sInfo.plist
file.You can force Launch Services to rebuild its database by running the following command in Terminal:
将事件处理程序代码移至
init
方法:注意:
myscheme
应采用x-com-companyname-appname
形式code> 这样它就不会与任何其他方案发生冲突。另请参阅:有关此主题的更多信息,请参阅如何将 Cocoa 应用程序设置为默认 Web 浏览器?
Move the event handler code to the
init
method:Note:
myscheme
should take the formx-com-companyname-appname
so that it never clashes with any other scheme out there.See Also: For more information on this topic see How do you set your Cocoa application as the default web browser?
更新数据库 OS10.8 Mountain Lion
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -f
-kill 在执行任何操作之前重置启动服务数据库别的
-seed 如果数据库未播种,则扫描应用程序的默认位置
和要注册的图书馆
-lint 注册包时打印有关 plist 错误的信息
-convert 注册在较旧的 LS 数据库文件中找到的应用程序
-lazy n 在注册/扫描之前休眠 n 秒
-r 递归目录扫描,不递归到包中或不可见
目录
-R 递归目录扫描,下降到包中并且不可见
目录
-f 强制更新注册,即使 mod 日期未更改
-u 取消注册而不是注册
-v 显示进度信息
-dump 注册后显示完整的数据库内容
-h 显示此帮助
Update database 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
显然,在沙箱下,您需要在 applicationWillFinishLaunching: 中注册,而不是 applicationDidFinishLaunching:
请参阅 Apple 文档。
Apparently under the sandbox you need to register in applicationWillFinishLaunching:, not applicationDidFinishLaunching:
See Apple's docs.