1

How can I create custom URL types that I can perform an action with the data passed into the app. I've tried this with no luck:

- (void)handleURLEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
atomikpanda
  • 1,845
  • 5
  • 33
  • 47

2 Answers2

3

You have to specify one or more keys for the CFBundleURLTypes dictionary in your app's property list file (or in XCode target editor).

Then in your app delegate applicationWillFinishLaunching write something like

NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
pronvit
  • 4,169
  • 1
  • 18
  • 27
  • Seems you still have to implement your handler method :) – pronvit Sep 24 '12 at 14:47
  • Ok it works now. but how can I make it so that if the URL is myapp://foo that it will preform an action and if it is myapp://bar it will preform a different action. – atomikpanda Sep 24 '12 at 19:23
  • @mifki Do you know if this would work on a sandboxed app? I understood that Apple Events were severely limited in a sandboxed environment. – trojanfoe Dec 05 '12 at 10:37
  • It will work. From documentation: "With App Sandbox, you can receive Apple events and respond to Apple events, but you cannot send Apple events to arbitrary apps. By using a temporary exception entitlement, you can enable the sending of Apple events to a list of specific apps that you specify, as described in Entitlement Key Reference." – pronvit Dec 05 '12 at 16:30
  • A more complete answer to this question including an example of the handler method is described here:http://stackoverflow.com/questions/1991072/how-to-handle-with-a-default-url-scheme - (void)handleURLEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent { NSString* url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; NSLog(@"%@", url); } – Dave Oct 02 '15 at 15:40
2
  1. Select your project in the left pane.
  2. Select your target in the list.
  3. Go to the Info tab.
  4. Add new URLs under URL Types.

enter image description here

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • thanks for helping get on the right track. I just need to know how to preform an action when the URL is opened. – atomikpanda Sep 24 '12 at 14:35