Last active
April 29, 2017 01:05
-
-
Save guoc/a7a00e6d4d1adc0b26c2aaf01b6f1fab to your computer and use it in GitHub Desktop.
Cocoa / macOS: Add URL Scheme
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<key>CFBundleURLTypes</key> | |
<array> | |
<dict> | |
<key>CFBundleURLName</key> | |
<string>add.your.identifier</string> | |
<key>CFBundleURLSchemes</key> | |
<array> | |
<string>urlschemename</string> | |
</array> | |
</dict> | |
</array> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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