15

I am creating an iOS version of an existing OSX app and am wondering what the best practices are for sharing code between the two. The code in question only depends on the foundation framework and the Sqlite dynamic library, so it should compile and run fine on both platforms.

It seems to me there are three possible options:

  1. Create a single project with and OSX and an IOS targets, add source files to each target as appropriate.
  2. Create two separate projects for the OSX and IOS apps, put shared code in a common location in the workspace and add it as reference to both projects.
  3. Create three projects: OSX app, IOS app and a shared static library with an OSX and an IOS targets; add each library target to the respective application.

Is there any reason one of the above approaches may be better than the other two? If not, option 2 seems to be the simplest by far.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
Alberto
  • 403
  • 3
  • 11

1 Answers1

5

I have done this and took the 3rd approach; a separate static library. The reason I chose that approach was because it can be kept within its own git repo and shared with other projects later on.

One thing to overcome, however, is determination of the platform you are compiling for. I solved this using the following code fragment which defines SYSINFO_IOS or SYSINFO_OSX which are then used within the library source/header files:

#import <TargetConditionals.h>

#if !TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR
#define SYSINFO_OSX 1
#else
#define SYSINFO_IOS 1
#endif

This fragment should be in its own header file.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242