I want to build two apps, one optimized for iphone and one for ipad.
both of these applications invoke server API calls and would share the same code for model objects, so I would like to share this code between my iphone and ipad projects.
I am using xCode 4, with a concept of workspace (which is supposed to allow sharing of code between projects) but I was wondering if I need to create another project for just the shared code.
Has anyone done this and care to share the best practices?
I prefer to share code across projects, be it for iPhone/iPad reuse or any other sharing scenario by leveraging a Static Library Project to house shared code, and used as a Target Dependency in all projects that consume it. In a bit more detail...
You can create a new Xcode Project with a Cocoa Touch Static Library template. This project can house all of the shared code between projects. Then, in any app in which you want to use the static library, you can add a project reference to the static library project by dragging the static lib project into the client project. In this client project, it's a good idea to add the static library as a Target Dependency build phase of the client project. This forces the shared library to be re-built anytime the client project is built. Then, you'll add the static library product to the "Link Binary With Libraries" build phase of the client project.
And, as one final caveat, you also need to tell the client project where the headers for the static library live. This is a result of static libs not containing their own headers like a Cocoa Framework on the Mac does. To do so, just find the "Header Search Path" build setting in the client project, and add the path to the static library headers. I've found it most useful to reference the ones produced by Xcode and placed in Derived Data (if you have Xcode configured to do so).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With