Skip to content

Instantly share code, notes, and snippets.

@spilliams
Last active December 15, 2015 14:19
Show Gist options
  • Save spilliams/5273740 to your computer and use it in GitHub Desktop.
Save spilliams/5273740 to your computer and use it in GitHub Desktop.

You're about to start a library or open-source thing that you want to be available to any iOS project. This is a handy guide for how to get set up.

#Setup

  1. start a new Xcode project with "Empty Application"
  2. project settings: verify name, devices (probably want Universal), Core Data (probably not), ARC (probably)
  3. choose location, don't add to any workspace or project.

Ok, project exists. What now? Let's rip out all that boilerplate Xcode so graciously gave us.

  1. remove the initial app target. We won't be building this to a device.
  2. remove the files relating to that initial app target (the entire group named after the target).

All that should be in your project now is a group called Frameworks with some things like UIKit and Foundation, and an empty group called Products. Good. We have our foundation, let's build something on it!

#Ground Floor

  1. Add a new target to the project, a Cocoa Touch Static Library
  2. enter a name for the product. I like to use the name of the whole project itself, unless I'm going to be creating multiple static libraries within this project. Keep in mind that Xcode will prepend "lib" to this name, so don't call it "libFoo" unless you want your static library to be "liblibFoo.a"
  3. In the Build Phases tab of the new product, remove the Copy Files phase, we don't need it.
  4. You'll probably end up using more than just Foundation, so link the binary with libraries UIKit and CoreGraphics.
  5. Add a new build phase, Copy Headers.
  6. You may have noticed when we created this build target that a new folder popped into your file system, containing one .h/.m pair and a .pch. Link the .h into the Copy Headers build phase (probably the Public section). You'll notice the .m is already linked into the Compile Sources.

Your static library should now successfully build.

the static library build phases
*note this image does not show test.pch, but it should be there!

#Moving Up

Now that we have a static library, we need an example app to show it off!

  1. Start an entirely separate Xcode project. New folder, new name, the works. How you set it up really depends on what kind of library you're providing, so I'll leave those details up to you.

  2. If your static library project is open, close it. Xcode can't have a project open in two places at once.

  3. Drag the static library .xcproj file from Finder into the example app's "Frameworks" group.

  4. In your example app's build phases, add a Target Dependency to the static library's build target.

  5. add the static library to the Link Binary With Libraries list.

    example app build phases

  6. One final step may be necessary, depending on how you structured your files. If the static lib project directory is not somewhere within the example app project directory you'll need to modify the example app's Header Search Paths (in Build Settings). Note: you can drag folders from Finder into the Header Search Paths pane:

    graphically adding a header search path

    Also keep in mind that the header names may not autocomplete when you're #importing them.

#To Infinity!

Once you're reasonably confident in your static library and example app, where do you go from there? Here are some ideas:

  • Write up a simple "how to use my library" in the README of the library
  • Update it to work in other environments, such as non-ARC or older versions of iOS
  • Document the shit out of it with appledoc
  • Release the library in a Cocoapod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment