Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@PierBover
Last active September 2, 2019 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PierBover/94659c0f04149e35bce3a0bd69320139 to your computer and use it in GitHub Desktop.
Save PierBover/94659c0f04149e35bce3a0bd69320139 to your computer and use it in GitHub Desktop.
Receive custom URL schema in macOS
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(self.handleGetURLEvent(event:replyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}
@objc func handleGetURLEvent(event: NSAppleEventDescriptor, replyEvent: NSAppleEventDescriptor) {
let urlString = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue!
let url = URL(string: urlString!)!
debugPrint(url);
}
}
@PierBover
Copy link
Author

Be sure to use urls with 2 slashes. So myscheme://something and not myscheme:something otherwise Safari will not recognize the scheme.

The Apple docs provide these examples for iOS which do not work in macOS:

myphotoapp:albumname?name=”albumname”
myphotoapp:albumname?index=1

https://developer.apple.com/documentation/uikit/inter-process_communication/allowing_apps_and_websites_to_link_to_your_content/defining_a_custom_url_scheme_for_your_app

You might need to close your browsers and then open your app before the scheme is recognized. Also, you might need to Product>Clean Build Folder in Xcode. At least when developing.

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