IPhone users will be familiar with URL schemes, especially Workflow, which uses URL schemes to their advantage. For those of you who are not familiar with URL Access Schemes, check out this article by The Minority.

Just like iOS apps, macOS apps can also support URL schemes. CurrencyX 1.4 has added support for the following three urls:

currencyx://x-callback-url/list? amount=100&base=cny currencyx://x-callback-url/trend? code=cny|usd currencyx://x-callback-url/search? key=cnyCopy the code

Users can use URL scheme to fill in data, view trend charts and search, which may not be useful for ordinary users, but some advanced app players should need.

The configuration Scheme

This is the same as in iOS, so no more fuss

Receive Apple Event

This step is not the same as iOS, but it is also very simple, directly to the code:

func applicationWillFinishLaunching(notification: NSNotification){ NSAppleEventManager.sharedAppleEventManager().setEventHandler(self, andSelector:#selector(handleGetURLEvent(_: withReplyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL)) } @objc func handleGetURLEvent(event: NSAppleEventDescriptor! , withReplyEvent: NSAppleEventDescriptor!) { if let urlString = event.paramDescriptorForKeyword(AEKeyword(keyDirectObject))? .stringValue, url = NSURL(string: urlString) {do what you want}}Copy the code

Pay attention to

You need to register it in applicationWillFinishLaunching event handler not applicationDidFinishLaunching, if the latter, Events received from URL Scheme when the App is not started will not enter your method.

x-callback-url

Currencyx :// XXX? X-callback-url (key=value) is used to communicate with apps, but without a standard, every developer would specify one for their app. X-callback-url is a standard for this. Apps that implement scheme control in compliance with this specification can happily call each other. [scheme]://[host]/[action]? [x-callback parameters]&[action parameters]

For details, visit X-callback-URL 1.0 DRAFT, R3

Third-party libraries

Once you understand how this works, a third-party library is recommended to do this: phimage/CallbackURLKit

The resources

  • URL Schemes use details
  • X-ray callback url – 1.0 DRAFT, R3

This article is simple, happy Coding 🍉

To support our

  • SalesX is a menubar tool for Apple developers. It will give you the sales status of your app as soon as possible, with a 7-day free trial
  • CurrencyX is a small and beautiful exchange rate app for Mac

If you find the article helpful to you, you can buy one to support us

Follow our official account to get the latest articles