Support `UIApplicationDelegate#application(_:open:options:)` on macOS for easier listening to custom URL schemes
Originator: | sindresorhus | ||
Number: | rdar://42800843 | Date Originated: | 01-Aug-2018 12:26 PM |
Status: | Closed | Resolved: | Yes |
Product: | macOS + SDK | Product Version: | macOS 10.13 |
Classification: | Feature (New) | Reproducible: | Always |
Summary: It would be nice if `UIApplicationDelegate#application(_:open:options:)`* could be supported on macOS as `NSApplicationDelegate#application(_:open:options:)`. To accept a custom URL scheme on macOS, we currently have to use some very archaic APIs and it would be beneficial to have consistency between platforms. * https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623112-application Steps to Reproduce: Expected Results: I expected it to work like on iOS: ```swift @NSApplicationMain final class AppDelegate: NSObject, NSApplicationDelegate { func application(_ app: NSApplication, open url: URL, options: [NSApplicationOpenURLOptionsKey: Any] = [:]) -> Bool { print(url) } } ``` Actual Results: But we currently have to do this ugly archaic boilerplate: ```swift @NSApplicationMain final class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_ notification: Notification) { NSAppleEventManager.shared().setEventHandler(self, andSelector:#selector(AppDelegate.handleGetURL(event:withReplyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL)) } func handleGetURL(event: NSAppleEventDescriptor!, withReplyEvent: NSAppleEventDescriptor!) { if let urlString = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue, let url = URL(string: urlString) { print(url) } } } ``` Version: macOS 10.13 Notes:
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!
Apple Developer Relations
This issue behaves as intended based on the following:
NSApplicationDelegate (in NSApplication.h) already has the following:
/* This will be called for any URLs your application is asked to open. This includes URL types (CFBundleURLTypes) defined in your Info.plist, and Document types (CFBundleDocumentTypes) that have no associated NSDocument class. Document URLs that have an associated NSDocument class will be opened through NSDocumentController. If this is implemented, application:openFiles: and application:openFile: will not be called. / - (void)application:(NSApplication )application openURLs:(NSArray *)urls NS_AVAILABLE_MAC(10_13);
In Swift, this translates to implementing the following on your app delegate:
func application(_ app: NSApplication, open urls: [URL])
(Note that this will only work if you do NOT set a custom event handler for kInternetEventClass/kAEGetURL via setEventHandler.)