Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handleGetURLEvent doesn't get called

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?

like image 229
tamasgal Avatar asked Feb 15 '12 07:02

tamasgal


2 Answers

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 /Applications folder, which should make Launch Services re-parse the app's Info.plist file.

You can force Launch Services to rebuild its database by running the following command in Terminal:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user
like image 164
Rob Keniger Avatar answered Nov 15 '22 06:11

Rob Keniger


Move the event handler code to the init method:

- (id) init
{   
    if ((self = [super init]))
    {
        NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
        [appleEventManager setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];

        // Add the following to set your app as the default for this scheme
        NSString * bundleID = [[NSBundle mainBundle] bundleIdentifier];
        LSSetDefaultHandlerForURLScheme((CFStringRef)@"myscheme", (CFStringRef)bundleID 
    }
return self;
}

Note: myscheme should take the form x-com-companyname-appname so that it never clashes with any other scheme out there.

See Also: For more information on this topic see How do you set your Cocoa application as the default web browser?

like image 23
David Kennedy of Zenopolis Avatar answered Nov 15 '22 06:11

David Kennedy of Zenopolis