我正在为我的某个应用实施自定义网址方案,但无法使其正常运行。
我将这些行添加到我的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://测试”
- (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);
}
我错过了什么?
答案 0 :(得分: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
答案 1 :(得分: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
形式,以便它永远不会与其他任何计划发生冲突。
另请参阅:有关此主题的详细信息,请参阅How do you set your Cocoa application as the default web browser?
答案 2 :(得分:2)
/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
答案 3 :(得分:1)
显然在沙箱下你需要在applicationWillFinishLaunching中注册,而不是applicationDidFinishLaunching:
请参阅Apple's docs。