If you are an iOS / MacOS developer who makes use of the snippets facility in Xcode you may occasionally find yourself caught out when working on a different Mac. This used to happen to me a lot in my contracting days. I have 10 years worth of snippets with auto completion keystrokes but on a Mac that doesn't belong to me they are worth nothing.
There is a really simple solution that makes use of Apple's cloud infrastructure which allows the sharing of Xcode snippets between as many machines as you like.
Here's how it's done.
We will use the relatively low tech but insanely useful Symbolic Link approach, essentially moving our snippets to our own personal iCloud account, then creating a link, or signpost if you like, that Xcode will follow with no complaint, maintenance free once configured.
The steps are easy to follow. be aware that Apple from time to time move where things live. So the snippets home today may not be where they live in 5 years from now, but I'm sure you can make the adjustment accordingly.
- Step 1
Close Xcode, open Terminal
- Step 2
Move the snippets folder.
Now this can be done using command line syntax, I prefer to do this manually, or at least take a back up of the contents in case of disaster.
Today my snippets live at:
~/Library/Developer/Xcode/UserData/CodeSnippets
This folder need to go, to vacate the space for the symbolic link, they can't share the same space.
The destination for this folder, needs to be in the iCloud folder, but, just for good house keeping and verbosity, needs to be one level down from the iCloud top level, which will become clear as to why later.
My path is this:
~/Library/Mobile\ Documents/com~apple~CloudDocs/XcodeSnippets/CodeSnippets
- Step 3
Creating the Symbolic Link.
on the command line the syntax for creating this follows the format of
The place I wish to link to
followed by
The place I wish the link to live
and looks like this
ln -s ~/Library/Mobile\ Documents/com~apple~CloudDocs/XcodeSnippets/CodeSnippets ~/Library/Developer/Xcode/UserData
running this in terminal will execute silently and just return you back to the prompt.
The reason why in the iCloud folder I have added the folder XcodeSnippets is, without it, we would be mapping the UserData folder in the Xcode path to the top level of the iCloud folder structure, not a good idea.
I could have called it UserData to match, but would you remember what UserData is in 3 years time without further examination? By calling it Xcode Snippets, there is no mistake or mystery what it contains. And it contains a folder that is named exactly how Xcode expects it to be named.
After completing the 3 simple steps, you can reopen Xcode and everything should work exactly as before, just, as you create or edit a snippet, it is saved to the cloud. Sweet!
New Machine.
When using Xcode on a new machine. The steps to make use of the snippets sync are, closing, Xcode, removing the CodeSnippets folder from
~/Library/Developer/Xcode/UserData/CodeSnippets
Then creating a symbolic link to your iCloud folder
ln -s ~/Library/Mobile\ Documents/com~apple~CloudDocs/XcodeSnippets/CodeSnippets ~/Library/Developer/Xcode/UserData
Comments