views:

200

answers:

2

I'm working on a Core Data application. I'd like to create interfaces on both the Mac and on iPhone, with both sharing the same model code.

Is it possible to set up an Xcode project to have both OS X (x86_64 10.6) and iPhone targets? Or is there a better way to do this?

+6  A: 

You can setup a single project with both iPhone and Mac targets, but there are a lot of gotchas and somethings don't work quite right. It is sort of lame that it works so poorly.

The best way to share model code is to separate it into its own project and build it as a static library, then include that in your mac and iPhone projects as a cross project link. It is. Here is a blog post explaining how to setup code sharing between multiple iPhone projects in detail. The same technique also works for sharing the static library in a mac app.

Louis Gerbarg
+2  A: 

Lets assume your application is called Foo.

  1. Create a new project using the Cocoa Framework template. I'd call this project FooCore.
  2. From the "External Frameworks and Libraries", delete the Cocoa.framework We remove this framework as it is not available on the iPhone. You may want to leave it so you can setup a test Target.
  3. Add a new Target, make this a Cocoa Touch static library.

    1. Double click the newly added target.
    2. Under the "Build" tab > Change "Configuration" pull down to "All Configurations".
    3. Change "Base SDK" to "iPhone Device 3.1" (Be sure to switch to "iPhone Simulator when building for the simulator)
    4. Change "Architectures" to "Standard"
  4. Add the frameworks you need to both targets. CoreData.framework
    Foundation.framework

Now you can build your project for inclusion with a Cocoa or Cocoa Touch project.

Brad Goss