Skip to content

Instantly share code, notes, and snippets.

@guoc
Last active April 29, 2017 01:05
Show Gist options
  • Save guoc/a7a00e6d4d1adc0b26c2aaf01b6f1fab to your computer and use it in GitHub Desktop.
Save guoc/a7a00e6d4d1adc0b26c2aaf01b6f1fab to your computer and use it in GitHub Desktop.
Cocoa / macOS: Add URL Scheme
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>add.your.identifier</string>
<key>CFBundleURLSchemes</key>
<array>
<string>urlschemename</string>
</array>
</dict>
</array>
// This is not necessary if you want the url scheme just open the app
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillFinishLaunching(_ notification: Notification) {
let appleEventManager: NSAppleEventManager = NSAppleEventManager.shared()
appleEventManager.setEventHandler(self, andSelector: #selector(handleGetURLEvent(_:withReplyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}
func handleGetURLEvent(_ event: NSAppleEventDescriptor, withReplyEvent: NSAppleEventDescriptor) {
if let urlString = event.forKeyword(AEKeyword(keyDirectObject))?.stringValue {
print(urlString)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment