Creating an Xcode4 Plugin
This tutorial describes the steps to create a very simple Xcode4 plugin project.
Note that there is no formal support for Xcode plugins, so this information may change and/or become obsolete as Apple makes changes to Xcode. Also, as it is unsupported, you will not be able to submit this plugin to the Mac App Store.
Overview
Although Xcode doesn’t officially support plugins, it does have some neat behaviour that allows us to inject our custom plugins. On startup, Xcode looks for bundles with an extension of .xcplugin
that are located in the ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins
directory (if present).
In order to create our plugin, we will be creating an Bundle project and then installing it into that location.
Creating the Project
This post was created using Xcode 4.5.1, however, I believe it should be appropriate for any Xcode 4.x installation.
Create Bundle Project
Click on the File/New/Project… menu item, and choose a Bundle in the OS X Framework and Library section:
and set your project name and other information. Choose Cocoa as the framework that you want to link to, and then save your project.
Modify Default Settings
Now that you have your basic project created, we need to clean up some things that we won’t use, and change a few settings so that it will generate a valid Xcode plugin.
Remove the following frameworks as they are not needed:
Cocoa.framework
CoreData.framework
You now need to add three new Boolean entries to your .plist
file:
XCGCReady = YES XCPluginHasUI = NO XC4Compatible = YES
Next up, we want to change the installation directory. The rationale for this is that every time you re-build the plugin, it will automatically get updated in-place in its eventual home, so you can re-test by simply restarting Xcode. Technically, I don’t think you need to do this, but it makes testing a lot easier.
Open up the Build settings for the BDXcodePluginDemo
target and change the following settings:
Setting | Declaration | Value | Notes |
---|---|---|---|
Installation Build Products Location | DSTROOT | $(HOME) | |
Installation Directory | INSTALL_PATH | /Library/Application Support/Developer/Shared/Xcode/Plug-ins | Note that it looks like the Installation Directory value is an *absolute* path but, in fact, Xcode appends that to the DSTROOT directory |
Deployment Location | DEPLOYMENT_LOCATION | YES | Tells Xcode not to use the Built Products location, and instead to use the installation directory |
Wrapper Extension | WRAPPER_EXTENSION | xcplugin | This is the plugin extension that Xcode is looking for |
Custom setting | GCC_ENABLE_OBJC_GC | supported | Xcode is a GC-enabled application, so our plugin also needs to be |
Adding Behaviour
So, now the project’s settings should be correct, but we don’t yet have any code. Add a new class called BDXcodePluginDemo
that inherits from NSObject
. Open the BDXcodePluginDemo.m
file, and add the following method:
+(void)pluginDidLoad:(NSBundle *)plugin { NSLog(@"This is our first Xcode plugin!"); }
Confirming Build
You should now be able to build the project, and check by running the following command:
ls -las $HOME/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins
If all goes well, you should see something like:
0 drwxr-xr-x 3 edwardsc staff 102 20 Oct 10:16 . 0 drwxr-xr-x 7 edwardsc staff 238 19 Oct 16:52 .. 0 drwxr-xr-x 3 edwardsc staff 102 20 Oct 10:16 BDXcodePluginDemo.xcplugin
Restarting Xcode
Run the following command and restart Xcode:
tail -f /var/log/system.log
You should see This is our first Xcode plugin!
appear in the log if all is successful.
Source Code
The source code and project for this article is available in the BDXcodePluginDemo project on GitHub.
That project contains the latest code in my series of plugin articles, but you can get the version that relates to this article using the creating-an-xcode4-plugin
tag.
What Next?
My next article will discuss a few common things that you might want to do. Specifically:
- Listening for notifications. This will give you an idea of the types of things that you can hook into.
- Adding menus. Depending on your plugin, you will probably want to be able to add a menu item.