Skip to content

Instantly share code, notes, and snippets.

@johnnyclem
Forked from mralexgray/customURL.m
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save johnnyclem/67a164337dad73fa62a2 to your computer and use it in GitHub Desktop.

Select an option

Save johnnyclem/67a164337dad73fa62a2 to your computer and use it in GitHub Desktop.

Revisions

  1. @mralexgray mralexgray created this gist Oct 19, 2012.
    13 changes: 13 additions & 0 deletions customURL.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    Here is what you need to do to register your app for a custom URL scheme (for the example we will use a "myapp" scheme).

    1) In your Info.plist, add a new entry for CFBundleURLTypes: <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLName</key> <string>MyApp's URL</string> <key>CFBundleURLSchemes</key> <array> <string>myapp</string> </array> </dict> </array>

    2) Somewhere in your application's startup code (e.g. init), add this code: - (void)registerMyApp { [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(getUrl:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL]; }

    - (void)getUrl:(NSAppleEventDescriptor )event withReplyEvent:(NSAppleEventDescriptor )replyEvent { NSString url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; // Now you can parse the URL and perform whatever action is needed }


    Related Tidbits:


    In Mac OS X Tiger and later, you can call LSSetDefaultHandlerForURLScheme to register an app as the default handler for a protocol, as would be need for a common protocol such as http, or ftp.