views:

645

answers:

2

Hi.
I want to build URI (or URL scheme) support in my app.

I do a LSSetDefaultHandlerForURLScheme() in my + (void)initialize and I setted the specific URL schemes also in my info.plist. So I have URL schemes without Apple Script or Apple Events.

When I call myScheme: in my favorit browser the system activates my app.

The problem is, how to handle the schemes when they are called. Or better said: How can I define what my app should do, when myScheme: is called.
Is there a special metod I have to implement or do I have to register one somewhere?

+3  A: 

As you are mentioning Apple Script, I suppose you are working on Mac OS X.
A simple way to register and use a custom URL scheme is to define the scheme in your .plist:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>URLHandlerTestApp</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>urlHandlerTestApp</string>
        </array>
    </dict>
</array>

To register the scheme, put this in your AppDelegate's initialization:

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

Whenever your application gets activated via URL scheme, the defined selector gets called.

A stub for the event-handling method, that shows how to get the URL string:

- (void)handleURLEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
{
    NSString* url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
    NSLog(@"%@", url);
}

Apple's documentation: Installing a Get URL Handler


On the iPhone, the easiest way to handle URL-scheme activation is, to implement UIApplicationDelegate's application:handleOpenURL: - Documentation

weichsel
+1  A: 

The problem is, how to handle the schemes when they are called.

That's where the Apple Events come in. When Launch Services wants your app to open a URL, it sends your app a kInternetEventClass/kAEGetURL event.

The Cocoa Scripting Guide uses this very task as an example of installing an event handler.

Peter Hosey