My Cocoa application must handle URL events. I think I followed the steps in this answer correctly to modify Info.plist and write and register the URL handler method.
Problems:
When I run mytesturl: // something in Safari while my application is running, a window appears asking if I want to open my application. And if I say yes, nothing happens, except that the icon appears for a moment in the dock. Maybe you are trying to start another instance of my application instead of sending a message to an executable instance?
When I do the same in Firefox, it opens a window asking you to select an application. So custom urls won't work in all browsers?
Info.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
...
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>My test URL</string>
<key>CFBundleURLSchemes</key>
<array>
<string>mytesturl</string>
</array>
</dict>
</array>
...
</dict>
</plist>
Source:
#import <Cocoa/Cocoa.h>
#include <stdio.h>
@interface Test : NSObject
{
}
- (void)test;
- (void)handleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent;
@end
@implementation Test
- (void)test
{
NSLog(@"Started...");
char c;
scanf("%c", &c);
}
- (void)handleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
NSURL *url = [NSURL URLWithString:[[event paramDescriptorForKeyword:keyDirectObject] stringValue]];
NSLog(@"url = %@", url);
}
@end
int main(int argc, char *argv[])
{
Test *app=[[Test alloc] init];
[[NSAppleEventManager sharedAppleEventManager] setEventHandler:app andSelector:@selector(handleEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
[app test];
return 0;
}
source
share