Skip to main content

Xcode Snippets shared in the Cloud

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



Reopen Xcode and all your snippets will be there, synced and ready to use. If you create a new one, or edit and existing one, the changes are also saved to the cloud. It's like your Xcode snippets environment is shared across as many Macs as you like.

And that's it, quick simple, effective. Enjoy!




Comments

Popular posts from this blog

The Winner Takes It All

I attended Scotch On The Road last week. A Coldfusion conference held throughout Europe and managed like a rock tour. It was a good day and I reacquainted myself with ex-colleagues from 3 different companies I have worked for. So it was nice to catch up with all. The session over the day were pretty specialist but a lot devoted to CF9, Adobe's new CFML server. Glad to say it's pretty good and a reasonable advance from CF8. I also won the raffle and have just finished choosing my $2000 worth of software, which will fit nicely onto the Macbook Pro.

I'm out of prison!

Actually I haven't been to prison I have been very busy working every hour I can in iOS development. I am now a iOS contractor with over 2 years experience under my belt and enjoying every minute of it.  I have decided to return to this blog and start sharing again the things I come across on my day to day travels.

Version Diversions

As a contractor I have to be light on my toes when it comes to using my own equipment and maintaining the ability to develop to the correct target version. Apple have recently made this a bit harder for the likes of me with the introduction of Xcode 4.5 (now at 4.5.2) The issue is - from Xcode 4.5, Apple dropped support for older devices that have the Arm processers Arm6. No big deal you may think. But a lot of my clients have clients that denote the minimum target iOS version to build for. This can cause problems and to compound things some directives specify iOS versions that are a) No longer supported by Apple, b) Have no device (both simulator nor handset) to test on. They are now starting to ask about updating their apps for the iPhone 5 resolution. It's hard to tell them that because of their target iOS, this can only be achieved by developing blindly relying on provisioning for device testing and the associated increase in development (known as cost) or increasing the ...