Created
September 15, 2016 01:46
-
-
Save anonymous/78ddfabbf80d25ac5a3fd43b6e42edbf to your computer and use it in GitHub Desktop.
Handle URIs with custom schemes on macOS
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
// After your application launches, register to handle Apple Events | |
func applicationDidFinishLaunching(_ aNotification: Notification) | |
{ | |
let appleEventManager = NSAppleEventManager.shared() | |
appleEventManager.setEventHandler(self, | |
andSelector: #selector(AppDelegate.handleGetURLEvent(_:withReplyEvent:)), | |
forEventClass: AEEventClass(kInternetEventClass), | |
andEventID: AEEventID(kAEGetURL)) | |
} | |
// When the Apple Event is handed to your app, get the URI from it | |
func handleGetURLEvent(_ event: NSAppleEventDescriptor, withReplyEvent replyEvent: NSAppleEventDescriptor) | |
{ | |
let uriString = event.paramDescriptor(forKeyword: keyDirectObject)?.stringValue! | |
print("\(uriString)") | |
} |
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>Fyrestead</string> | |
<key>CFBundleURLSchemes</key> | |
<array> | |
<string>fyrestead</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
import Cocoa | |
NSWorkspace.shared().open(URL(string: "yourscheme://path/to/whatever")!) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment