BWHazel
BWHazel

Reputation: 1474

Can preprocessor directives be used to import different header files for Mac and iOS?

I am writing a class library for Mac OS X and iOS to be released as a Cocoa Framework for OS X and a Static Library for iOS. To simplify matters, I intend to use multiple targets in Xcode. However, the classes on Mac OS X link against Cocoa.h whereas on iOS they link against Foundation.h.

My questions basically are:

Upvotes: 8

Views: 7754

Answers (4)

Tibidabo
Tibidabo

Reputation: 21571

This works perfectly for me:

#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
//iOS 
#else
//Mac
#endif

Upvotes: 1

iuriimoz
iuriimoz

Reputation: 1081

It works like a charm:

#ifdef __APPLE__
  #include "TargetConditionals.h"

  #if TARGET_IPHONE_SIMULATOR  
  // ios simulator

  #elif TARGET_OS_IPHONE
  // ios device

  #elif TARGET_OS_MAC
  // mac os 

  #else
  // Unsupported platform
  #endif
#endif

Upvotes: 8

detunized
detunized

Reputation: 15289

You can use these to separate platform dependent code (see TargetConditionals.h):

#ifdef TARGET_OS_IPHONE 
    // iOS
#elif defined TARGET_IPHONE_SIMULATOR
    // iOS Simulator
#elif defined TARGET_OS_MAC
    // Other kinds of Mac OS
#else
    // Unsupported platform
#endif

Here's a useful chart.

Upvotes: 21

Cajunluke
Cajunluke

Reputation: 3113

  • Could the Mac OS X framework link against Foundation.framework instead? Classes used within the framework are NSString, NSMutableString, and NSMutableArray.

Try it and see. If the compile fails, no. If it succeeds, yes.

  • Or could I use preprocessor directives within the header files to control framework inclusion, e.g.

Yes, you can. In fact, I believe that is the only way to do that.

Upvotes: 2

Related Questions