Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there best practices for sharing development code between iPhone and iPad app

Tags:

iphone

ipad

ios4

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?

like image 279
Kevin Liang Avatar asked Oct 10 '22 08:10

Kevin Liang


1 Answers

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).

like image 138
Matt Wilding Avatar answered Oct 13 '22 03:10

Matt Wilding