TigerCoding
TigerCoding

Reputation: 8720

Which project template should I use for an iOS and Cocoa library?

I have a bunch of code in an iOS project (named "MyLibs") I re-use across different apps. I drag the MyLibs project into the workspace of whatever app I'm creating. I don't use unit tests per se, but I have buttons than run through all the tests very easily in the iPhone app.

I'm learning Cocoa and would like to divide up my library into libs I can use on both projects. I was thinking it would be MyCommonLibs (or MyFoundationLibs), MyIOSLibs and MyCocoaLibs.

However, when creating a new project, I must choose between an iOS app or a Cocoa app. It looks as though the iOS Framework and Library -> Cocoa Touch Static Library is appropriate because it links against the Foundation framework. On the other hand, I'd prefer to use an application template if there is no major drawbacks to it.

I need to be able to use MyCommonLibs in both app types, as some of them are useful to both, such as NSArray categories, etc.

Which template should I use for the MyCommonLibs and must I use a Library, Framework (in the Mac OS templates) or can I just use a normal application template (as I've been doing thus far)?

Upvotes: 2

Views: 306

Answers (1)

sch
sch

Reputation: 27506

  1. Create an iOS Framework & Library project. Let's call it TestLib.

  2. Add a new target (File > New > Target) of type MAC OS X Framework & Library.

This way, you can compile both an iOS library and a MAC OS X library from the same project.

You can choose which files are included in each target. So if you want to make a class available for both iOS and MAC OS X, you add it to both, and if you want to make it available for only one platform, you can add it to only one lib.

As you can see in the screenshots below, SharedClass is available in both libs, iOSOnlyClass is available for iOS only and MACOnlyClass is available for MAC OS X only.

enter image description here

enter image description here

You can also add targets for unit tests in the same project.

To organize your code, you can put the shared classes in a group, and the classes of each target in a separate group.

enter image description here

Upvotes: 2

Related Questions