Skip to content

Instantly share code, notes, and snippets.

@mralexgray
Created October 19, 2012 00:33
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mralexgray/3915592 to your computer and use it in GitHub Desktop.
Save mralexgray/3915592 to your computer and use it in GitHub Desktop.
Register Cocoa app for a custom URL scheme
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.
@preble
Copy link

preble commented Jul 18, 2018

macOS 10.13 simplifies this by adding the NSApplicationDelegate method func application(_ application: NSApplication, open urls: [URL])

@MajuMadhusudanan
Copy link

i'm trying to register custom URL for app targeted for 10.13 and above. But when system throws a warning message saying "no default application associated for custom protocol - abc://' and ask user to select the app. If user select it then it wont prompt again. ANy idea why its not registering custom protocol?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment