They say that you should not duplicate your code, but when it comes to iOS development what I see is a lot of duplicated files from third party libraries… For example, JSON and ASIHTTPRequest. So we end up with loads of duplicated files or, if not duplicated, very similar, differing in version.
Static Libraries would help the programmer with this problem.
To create your static library project, you choose the Cocoa Touch Static Library template under Framework & Library. What you need to note about this project is:
- Set the headers (you want to be public) to
public. This can be set during theCopy HeadersunderBuild Phasesof your static library target. - Set the
Installation Directoryto$(BUILT_PRODUCTS_DIR). Skip Installshould be set toYes.Public Headers Folder Pathshould be set to$(TARGET_NAME).
This project can then be used by other projects. I recommend you to set up a workspace for this. You can either save a current project as a workspace or create a workspace and drag and drop your project (the .xcodeproj file) to it. Be careful when you drag the static library project not to drag it inside another project. It should be at the same level of the main project.
Then, follow these steps for your project:
- Add your library to
Link Binary With Libraries. - Set
User Header Search Pathsto$(BUILT_PRODUCTS_DIR)recursively. - You may need to add
-ObjCtoOther Linker Flagsif you use categories. You may also need to add-all_loadand-force_loadin some situations.