Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for sharing code between OSX and IOS app

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.

like image 915
Alberto Avatar asked Sep 17 '12 07:09

Alberto


People also ask

How do I code iOS apps on Mac?

To develop iOS apps, you need a Mac computer running the latest version of Xcode. Xcode is Apple's IDE (Integrated Development Environment) for both Mac and iOS apps. Xcode is the graphical interface you'll use to write iOS apps.

Can I code for iOS without a Mac?

With Flutter and Codemagic, you can build and distribute iOS apps without buying a Mac computer yourself. In this post, we will walk you through how you can create a Flutter app on Linux or Windows and use Codemagic CI/CD to set up code signing for your iOS project and release the application to the App Store.

Can you code an iOS app without Xcode?

Non-native platforms, like Flutter or React Native, won't make iOS builds without Mac either. Storyboards can be edited only in Xcode, so development without Xcode means development without Storyboards. Alternative IDEs for iOS development require Xcode. You don't need to run it, but you should have it installed.

Is it hard to code an iOS app?

You're in luck – developing an iOS app is not hard. In fact, there are numerous tools that make developing your own iOS app easy and fun. Armed with a little knowledge and these tools, you too can learn to code iOS apps! This tutorial series will teach you how to make an iOS app from scratch.


1 Answers

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.

like image 94
trojanfoe Avatar answered Oct 23 '22 10:10

trojanfoe