Reusing files across iOS projects


Throughout each project, I add and tweak reusable files, such as categories and view classes. However, I have certain issues to deal with: how do I get the old files prefixes to match this new project? How can I move settings from one project to another? In this post, I’ll show you how I do things, and hopefully you can use some pieces of it for when you’re starting your next iOS project.


Dealing with prefixes

I like my reusable files to have the same prefix as the current project I’m working on. For example, if I make a category file for my VOX project with the prefix VOX, NSString+VOX.m, I want to reuse it in my LIV project as NSString+LIV.m. To do this, I keep a folder of all of my reusable files that I copy into each new project. Everything in this folder is prefixed with PREF. Then, I replace all occurrences of PREF in my files and file names, using this code, which I keep in my .bashrc file:

replace_text () { for file in $(grep -rl “$1″ .)
sed -e “s/$1/$2/g” $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
done ; }

replace_file_names () { for i in `find . `
do mv “$i” `echo “$i” | sed -e s/$1/$2/g`;

batch_replace () { replace_text $1 $2 ; replace_file_names $1 $2 ; }

To run this, I call `batch_replace PREF ROL` if I want to set it to ROL. It’s not perfect, but it generally works. When the project is done, I move all of the files from the reusable code folders back into my shared files folder, run a batch_replace command to set them back to PREF, and then push my new folder to git. Here’s my git repo:


What to reuse?

In general, I try to reuse my settings (.xcconfig files), my .gitignore, my Podfile with some common pods, and lots of Objective-C files, such as my beloved categories. Try out my UIView+PREF.m, NSObject+PREF.m, and NSString+PREF.m files if you’re curious about cool uses of categories!


Putting it all together

Starting a project:

  1. Make a new project in Xcode
  2. copy all of the files in ~/xcode_shared_…/ over to the project (before actually dragging them into xcode, so we can do the replace first!)
  3. run `batch_replace PREF <prefix>`
  4. Drag all of the folders and files into xcode. Make sure that they’re in the right spots, e.g., .gitignore and Podfile should be in the project root.
  5. install cocoa pods by moving .cocoapods into the main folder, making sure the correct iOS and stuff are specified at the top of the file, then running `pod install`
  6. open <app name>.xcworkspace
  7. install crashlytics, mixpanel, and parse (be careful in following the crashlytics steps)
  8. add the following step to the build phase storyboardlint “$SRCROOT” –storyboard-suffix Storyboard –segue-suffix Segue –reuse-suffix ReuseID
  9. use the two .xcconfig files to get the settings right (AFTER cocoa pods is installed)

Finishing a project:

  1. Copy all of the files from shared folders, e.g. the Categories folder, back into the shared files folder
  2. Copy the .xcconfig files over, if there were changes I made that I want to reuse
  3. Remove all of the files from Podfile that I don’t want to reuse
  4. Run a `batch_replace {prefix} PREF`
  5. git push the new changes