If you want to launch your app from a URL, Cocoa provided a nice way of doing it.
First, you have to register for the event. You can do it anytime, but I put this code on my AppDelegate class:
First, you have to register for the event. You can do it anytime, but I put this code on my AppDelegate class:
open func applicationWillFinishLaunching(_ notification: Notification) { initializeURIOptions() } fileprivate func initializeURIOptions() { // register to listen to the url event let appleEventManager = NSAppleEventManager.shared() appleEventManager.setEventHandler(self, andSelector: #selector(handleGetURLEvent(event:withReplyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL)) }
Then, we have to actually handle the URL parameters. In this example, we assume that the URL is of the form myapp://param1=val1¶m2=val2
So we must parse the URL and extract the "param=value" pairs
So we must parse the URL and extract the "param=value" pairs
@objc fileprivate func handleGetURLEvent(event: NSAppleEventDescriptor?, withReplyEvent replyEvent: NSAppleEventDescriptor?) { guard let event = event else { return } // our URL event is in this format: // myapp://param1=val1¶m2=val2¶mN=valN // grab the query pairs, that is the pairs separated by '&' guard let paramDesc = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue, let url = NSURL(string: paramDesc), let queryPairs = url.query?.components(separatedBy: "&") else { return } // parse the key/value pair // querypairs is assumed to be in the form of "key=value" var settingPairs = [String: String]() for query in queryPairs { let kvp = query.components(separatedBy: "=") if kvp.count != 2 { continue } let key = kvp[0].lowercased() let value = kvp[1].lowercased() settingPairs[key] = value } // do stuff with settingPairs }
Finally, we have to let the OS know to associate the "myapp" URL scheme with our app.
We can do this by editing the app's info.plist and adding a URL Schemes item under the URL types item. The value for this key is the scheme that the OS will associate with your app.
We can do this by editing the app's info.plist and adding a URL Schemes item under the URL types item. The value for this key is the scheme that the OS will associate with your app.
Now, you should be able to launch your app by going to Safari (or any web browser and Terminal), and typing myapp://
If you want to pass parameters to the app you can simply add them to the URL myapp://?param1=val1¶m2=val2
When you do this from Safari, you will get a notification such as this:
If you want to pass parameters to the app you can simply add them to the URL myapp://?param1=val1¶m2=val2
When you do this from Safari, you will get a notification such as this:
To debug an app that is launched this way, you can modify XCode's setting to wait until your app is launched before attaching the debugger to the app.
XCode will now automatically attach the debugger to your app when you launch your app via the URL myapp://