-
-
Save ShonFrazier/6ba66510716ce27730fde75ba88df861 to your computer and use it in GitHub Desktop.
Handle URIs with custom schemes on macOS
This file contains 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 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>MyScheme</string> | |
<key>CFBundleURLSchemes</key> | |
<array> | |
<string>myscheme</string> | |
</array> | |
</dict> | |
</array> |
This file contains 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: "myscheme://path/to/whatever")!) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In your Info.plist, add the content above to the top-level dictionary