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
- start a new Xcode project with "Empty Application"
- project settings: verify name, devices (probably want Universal), Core Data (probably not), ARC (probably)
- 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.
- remove the initial app target. We won't be building this to a device.
- 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
- Add a new target to the project, a Cocoa Touch Static Library
- 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"
- In the Build Phases tab of the new product, remove the Copy Files phase, we don't need it.
- You'll probably end up using more than just Foundation, so link the binary with libraries UIKit and CoreGraphics.
- Add a new build phase, Copy Headers.
- 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.
*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!
-
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.
-
If your static library project is open, close it. Xcode can't have a project open in two places at once.
-
Drag the static library .xcproj file from Finder into the example app's "Frameworks" group.
-
In your example app's build phases, add a Target Dependency to the static library's build target.
-
add the static library to the Link Binary With Libraries list.
-
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:
Also keep in mind that the header names may not autocomplete when you're
#import
ing 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: