I’m implementing a custom URL scheme for one of my apps and can’t get it to work.
I added these lines to my Info.plist:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>MyApp URL</string>
<key>CFBundleURLSchemes</key>
<array>
<string>myscheme</string>
</array>
</dict>
</array>
In my application delegate I install the event handler in ApplicationDidFinishedLaunching:
NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
but the method isn’t called when I click on a link with the URL eg. “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);
}
What did I miss?
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
/Applicationsfolder, which should make Launch Services re-parse the app’sInfo.plistfile.You can force Launch Services to rebuild its database by running the following command in Terminal: