Not many people know that Apple introduced some interesting automation capabilities in Xcode 9 via URL schemes. I sometimes use them, and as I didn’t see them publicized anywhere, I decided to document them in this blog post.

Source Code Navigation

The new Xcode source editor, written in Swift, has a neat way to link between documentation and source code locations inside a project. First of all, you need to put this comment on top of the target location where you want to go, for example, on top of a method:

/// - Tag: MyAwesomeMethod func myAwesomeMethod(...) { ... }

After that, create a Markdown link in a file placed inside the project (the typical README file is a good candidate for this):

[View in Source](x-source-tag://MyAwesomeMethod)

Using this technique, a person who reads your README file and clicks on “View in Source” will navigate directly to the implementation of your awesome method. You can even open this tag URL in your browser and it will bring Xcode to the foreground and navigate to that symbol!

You can see that Apple has been silently using this feature in their recent sample code: https://developer.apple.com/library/content/samplecode/AudioInARKit/Introduction/Intro.html#//apple_ref/doc/uid/TP40017668-Intro-DontLinkElementID_2

Git

This is more or less a well-known feature of Xcode 9 (as it was announced in WWDC 2017): You can automatically clone repositories from GitHub by clicking on “Open in Xcode” in a repository that contains an Xcode project or workspace:

This feature is also supported by a URL scheme:

xcode://clone?repo=<URL_encoded_repository>

This will launch Xcode, clone the given repository, and open its main project or workspace, all in one step.

Hopefully, Apple will support git branches in this URL scheme in a future version of Xcode. The perfect use case is opening a code review in your IDE, directly from GitHub.

Devices and Simulators Management

I happen to open the Devices and Simulators pane in Xcode quite frequently (accessible from Window, Devices and Simulators), typically to open crash logs. This is the URL scheme that I use to automate this step and manage my devices more quickly:

xcdevice://showDevicesWindow

This URL scheme also accepts a parameter to automatically enable a particular device for development:

xcdevice://enableForDevelopment?identifier=<Device_Identifier>

Quick Access to Preferences

If I need to open quickly a particular tab in Xcode preferences (say Key Bindings or Components), I also use a URL scheme:

xcpref://GeneralPrefs (General tab) xcpref://AccountsPrefs (Accounts tab) xcpref://AlertPrefs (Behaviors tab) xcpref://KeyBindingsPrefs (Key Bindings tab) xcpref://FontAndColorPrefs (Font and Colors tab) xcpref://NavigationPrefs (Navigation tab) xcpref://LocationsPrefs (Location tab)

And that’s it, I hope you like these tips and hopefully streamline your Xcode workflow a little bit. Enjoy!