Last active
September 2, 2019 17:22
-
-
Save PierBover/94659c0f04149e35bce3a0bd69320139 to your computer and use it in GitHub Desktop.
Receive custom URL schema in 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
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Be sure to use urls with 2 slashes. So
myscheme://something
and notmyscheme:something
otherwise Safari will not recognize the scheme.The Apple docs provide these examples for iOS which do not work in macOS:
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.