Cocoa dev: Design your own Xcode project templates

When you create new projects, Xcode prompts you to select a template to start from. From here, you can choose from iPhone and Mac OS X options. Each option lets you craft your application from a pre-designed program skeleton. For the iPhone, these include view-based applications and applications built with OpenGL ES. For the Mac, you can build dynamic libraries, command line utilities, and apps built with Cocoa, among many others.

Sometimes, though, you find yourself taking the same steps over and over to customize these skeletal projects to your own particular in-house design. That's time and effort that you could better spend doing real development, however. Fortunately, Xcode supports user-defined templates that you can pre-customize so you can always start your new projects off where you really need to begin, not just where Apple left off.

Jay Abbot of TinyPlay.com posted a handy how-to over at the iPhone dev FAQ showing exactly how to do this. His instructions basically involve making a copy of one of Apple's templates, dragging it to a folder in your application support library, and customizing it.

Apple stores its project templates in Xcode's /Developer directory. iPhone project templates are found in

/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application

Each folder in that directory corresponds to a single template. Copy whichever template best matches your application type onto your desktop. The folder contains a number of files, which you will customize to create a template that matches your needs.

Locate the Library/Application Support/Developer/Shared/Xcode folder in your home directory. In that folder, create a Project Templates folder. This folder is meant to contain groups of templates. Just as the Mac OS X templates include headings like "Application," "Audio Units," "Automator Action," "Bundle," and so forth, the subfolders you add to the Project Templates folder create the unified headers shown in the left column of the New Project window. The iPhone templates contain only one group: Application, which is where you copied your template from in the first place.

Decide on a group name for your new template (I used "Testbeds") and drag the copied template from desktop into the group folder. Rename its folder meaningfully. The name of the folder corresponds to the name of the template shown in Xcode.

Navigate into your new template folder, and open ___PROJECTNAME___.xcodeproj. Do not edit the name, as the ___PROJECTNAME___ variable is set when creating new Xcode projects.Edit the project in Xcode to set it up however you like. You can add, delete, or edit files however you want. For example, you may want to edit the Info.plist file so that your company name is always set properly in the application identifier (versus com.yourcompany). You can also add your Distribution and Ad Hoc configurations, including your ad-hoc entitlement. Bring the project up to what you consider to be the best starting/jumping off point: specific enough to save you work, and not too specific that you don't have to do any extra work to make it usable for other projects.

Make sure that the template actually compiles and, if working with iPhone source, that it runs properly in the Simulator. Save your work and then delete the build folder. Also delete the user-specific files in the xcodeproj subdirectory that contain your user name, especially if you intend to share your template with others. As Abbot points out, you'll need to delete these files again should you ever re-edit your project. I created a simple shell script in my Testbeds folder that automates the clean-ups.

To finish, update the template description in the TemplateInfo.plist in the xcodeproj folder and, optionally, change the images in the TemplateIcons.icns file. Xcode ships with an icon editor that lets you paste art into icns files if you like. Otherwise, the icon defaults to the standard image used by the template you copied.

After following these steps, you'll have created custom templates that you can use in Xcode to start new projects. To test, go to Xcode and start to create a new project. Your custom templates should appear in a User Templates section in the left column.

Needless to say, custom templates can save you a lot of work. By carefully going through the project initialization process once, you can build on that well-executed start for all your projects. Share these templates with others by zipping up their folders. It's probably best to zip starting at the template group level and then drop them into Project Templates folders.

posted @ 2010-08-13 10:16  harvey.ding  阅读(318)  评论(0编辑  收藏  举报