19

I'm working on an Cocoa app which is launched/activated using URLs with a custom scheme which is registered in the Info.plist file like so:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>Open myscheme:// URLs</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>myscheme</string>
        </array>
    </dict>
</array>

My question is, once the app is launched or activated, how do I tell what the URL was that launched the app? On iOS, this is easy with the -application:openURL:sourceApplication:annotation: method on the UIApplicationDelegate since it is passed an NSURL instance.

I want to be able to pass in data into my app with URLs like myscheme://do/something/awesome

albert
  • 8,112
  • 3
  • 47
  • 63
phatblat
  • 3,804
  • 3
  • 33
  • 31

1 Answers1

24

In your app delegate's -applicationWillFinishLaunching:, do:

[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(handleAppleEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];

And handleAppleEvent:withReplyEvent: should look something like:

- (void)handleAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
    NSString *urlString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
    // do something with the URL string
}
joshaber
  • 2,465
  • 20
  • 13
  • What is keyDirectObject and where is it defined? I don't understand what the value of keyDirectObject is supposed to be. Thanks. – Joel Nov 19 '14 at 22:53
  • 1
    For those that are wondering like me, "How does keyDirectObject compile without syntax error for a missing symbol?", it's defined in AE/AppleEvents.h as an enum case. Somehow AE/AppleEvents.h gets included in your app even if you don't directly include it. – Joel Nov 19 '14 at 23:05