macosurllaunchappleevents

Catch registered URL scheme and launch app


I managed to launch my application whenever a URL scheme is called via the browser, by putting this scheme in the info.plist and adding these 2 lines:

NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self andSelector:@selector(getUrl:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];

and correctly implementing my getUrl function.

So I tried putting the two lines above in my appDelegates's applicationWillFinishLaunching, applicationDidFinishLaunching and init. But in the three cases, I have the same scenario: if my app is already running, then the getUrl function is correctly called, by if the app is not launched, then getUrl is never called.

I found on other posts (How to handle with a default URL scheme and get url event on app open in objective c (Mac OSX)) but the suggestion which comes often is using the applicationWillFinishLaunching, which didn't work in my case.

Edit: here is my info.plist: http://pastebin.com/yM8zabvY


Solution

  • I'm not sure where things are going sour for you. I've set up a test project, in which I just pasted the CFBundleURLTypes key from your Info.plist, and I used this in my app delegate:

    -(void)getUrl:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor*)reply
    {
        NSLog(@"%@", event);
    }
    
    -(void)applicationWillFinishLaunching:(NSNotification *)aNotification
    {
        // Register ourselves as a URL handler for this URL
        [[NSAppleEventManager sharedAppleEventManager]
         setEventHandler:self
         andSelector:@selector(getUrl:withReplyEvent:)
         forEventClass:kInternetEventClass
         andEventID:kAEGetURL];
    }
    

    And it works just like I'd expect. I get the log message regardless of whether the app was already running or not when I run open "feed://test" in the terminal.