5

I need to support iOS and OSX in my shared library, whats the best Macro / Practise for conditioning iOS and OSX code

the bellow code doesn't work for some reason :/

- (NSString *)hostName {
    #ifdef TARGET_OS_IPHONE
        return [[UIDevice currentDevice] name];
    #elif TARGET_OS_MAC
        return [[NSHost currentHost] localizedName];
    #endif
}
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
  • Are you looking to build two separate libraries (which is what you need to do with the code posted in your question), or are you looking for a runtime solution? – rmaddy Jan 18 '15 at 18:33
  • Compiler directives can't be used for runtime checks. And I don't think you can build a shared library that supports both iOS and OS X. I believe you need two separate libraries since the processors are quite different. You might be able to build a single framework with separate slides for each. But either way, you end up compiling each separately. – rmaddy Jan 18 '15 at 18:52

1 Answers1

9

Two issues:

  1. Use #if rather than #ifdef
  2. Check TARGET_OS_IPHONE first since TARGET_OS_MAC is defined to 1 for both platforms

See: Which conditional compile to use to switch between Mac and iPhone specific code?

Community
  • 1
  • 1
EricS
  • 9,650
  • 2
  • 38
  • 34