Sharing code between ios and os x apps

Sharing code between ios and os x apps

Written By: Debasis Das (25-Feb-2015)

In this article we will exhibit how we can build an iOS and Mac OS X app for Sharing code between ios and os x apps ensuring maximum code reusability.

We will also learn what can and cannot be reused across both the platforms and what should be the design principle to achieve maximum code reusability across platforms.

Some extra work for organizing the code, creating reusable frameworks targets will ensure that code is now reusable across platform which was otherwise required to be built separately for two OS specific applications

What code CAN BE and CANNOT BE Shared across the iOS and OS X Platforms

Model Code:

  • The models, business entities can be completely reused.

View Code:

  • There are some problems in sharing the view code.
  • Interaction:
    • The way users interact with the application differs in iOS and OS X Platforms
    • In iOS user interact with the system through touch gestures. Touch is tracked in a 44 * 44 pixel area, where as in Mac OSX, the interaction is different (via, Mouse events , hot keys, keyboard events etc). Cursor in Mac OSX is precise single pointer as compared to finger touch in iOS.
  • Display Size:
    • Real estate in iPhone vs iPad vs Mac plays an important role in the view design for both the platforms
    • Available Screen Size – Single view in iOS vs Multiple Apps & Multiple windows of each app in Mac OSX
  • Compilation Issues
    • UIWebView vs WebView
    • UIView vs NSView

One possible solution to the above stated problem is to do the following

@interface MyCustomView : UIView
# else
@interface MyCustomView : NSView
# endif

The above approach although being a possible solution will eventually lead to hard to read and hard to maintain code

Summary: The view code must be platform specific

Controller Code:

  • Some of the controllers can be shared across iOS and OSX.
  • Controllers such as View Controllers are tightly coupled to the view itself and thus reuse across platforms should not be attempted as a guideline
  • Ideally any controller that is platform specific should not be attempted to be reused.
  • The Controllers which can be reused
    • Model Controllers
    • Controllers that drive business logic
    • Controllers that manage the state of objects

A summary of what CAN be Shared.

  • Classes that are subclasses of the following
    • Core Foundation Classes
    • Foundation Classes
    • Core Data
    • Core Text
    • Core Animation
    • Core Graphics (After flipping the context for origin)
    • Core Image
  • Model Objects
  • Model Controllers
  • Business Logic Controller
  • Controllers that manage the state of objects

A summary of what CANNOT be Shared.

  • Appkit vs UIKit Specific Subclasses
  • View Controllers


Sharing code between ios and os x apps

Sharing code between ios and os x apps

Project Setup for reusing code across OSX and iOS Apps

Step 1.a : Create a new iOS Application (here we are using a simple single view iOS Application)


Step 1.b : Here we are naming the app as KStackIOS

Step 2.a : Create a new Mac Application Target and will call it KStackMac

Step 2.b: In the below screenshot we see that both the targets has been created

Step 3.a: Create a New Project  (Cocoa Touch Framework) as part of our original project 

Step 3.b: We will name this project as KStackSharedFramework (This will eventually have 2 target framework one for Mac and the second one for iOS)

Step 3.c: In the below screenshot we see that a target has been created for the Cocoa Touch Framework

Step 4.a: Create a new Mac Framework (Cocoa Framework) Target

Step 4.b: We will name the Cocoa Framework as KStackSharedFrameworkMac

Step 5: In the below screenshot we will add two files namely Employee.h and Employee.m (Employee class is a subclass of NSObject and has two attributes firstName and lastName)

Step 5.b: Adding Employee Class to the Shared Framework

Step 6: We will modify the Public Headers Folder Path to KStackShared for both the iOS and Mac Framework Targets

Step 7: Modify the Search Path in the Mac and iOS App Targets to $SRCROOT

Step 8: Include the KStackSharedFrameworkMac in the Mac App Target

Step 9: Repeat Step 6 for the iOS App

Download the code here

Posted in Cocoa, Cocoa Touch, Design Pattern, iOS, Objective C Tagged with: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *