When developing different iOS projects, you will most likely want to keep common code in a shared library that each individual project can make use of. A useful way of doing this is to package your shared code as a static library which can then be imported into each application project. One problem I had previously experienced when building my own static library a long time ago was the difficulty in bundling resources such as nib files within the library. I wanted to be able to share common code, but what good was it if the library wasn’t going to include all the bits and pieces I wanted, right?!
Fortunately, there is a great post available here which describes exactly how one can go about building a static library with resources for iOS. I’ve followed the process myself and it works really well. Once setup is complete, as part of building your project it will also build your library and link your project object files against the static library.
Some things to take note of:
- When performing step 7 (Linking with the Library) of the static library process is to ensure that your library xcode project is closed when attempting to drag the library project from finder into your project navigation pane, otherwise it may not import correctly.
- When adding new files to your static library, ensure that the header files are added to the Copy Files build phase of your target.
CocoaPods is a neat tool for managing library dependencies in an Objective-C project. Just like Apache Maven, CocoaPods allows one to specify dependencies in a configuration file and will automatically fetch, install and configure the project with those dependencies.
When using a static library and CocoaPods with your iOS projects, it may be the case that your application project and static library make use of the same 3rd party external libraries. You may think to yourself that you will setup your CocoaPods configuration so that only your static library project includes the 3rd party libraries, in hopes that these will automatically be linked in when building your main project against your static library, however, this won’t work given the way CocoaPods structures your workspace. A static library is essentially just an archive of object files that ultimately get linked to create a final binary/application, so you need to explicitly include the 3rd party libraries in your application project for the linking to take place at build time.
Below are high-level steps to setup a workspace whereby a static library and application project make use of the same 3rd party library, for instance the AFNetworking library:
- Create a static library project LibProject as shown above.
- In the root folder of LibProject, create a CocoaPods configuration file Podfile with the following contents:
platform :ios, ‘6.0’
pod ‘AFNetworking’, ‘1.0’
- Using the terminal, execute the command ‘pod install‘ in the root folder of LibProject.
- Import the static library project LibProject into your main application project AppProject as explained in the first section of this post.
- In the root folder of Approject, create a CocoaPods configuration file Podfile with the same contents as above.
- Using the terminal, execute the command ‘pod install‘ in the root folder of Approject.
- Make sure to open your project using AppProject.xcworkspace instead of AppProject.xcodeproj.
- Hopefully your project can now make use of AFNetworking and build correctly 🙂
CocoaPods Tip: whenever using ‘pod install‘ to setup your Cocoapods dependencies for a project, make sure not to use the superuser, i.e. ‘sudo pod install‘… this will lead to nasty permission problems later on!