acb's technical journal

Posts matching tags 'modularity'

2018/1/17

Cross-platform code sharing with Carthage made easy

Imagine for a moment that you're developing applications for both iOS and macOS (and perhaps an Apple Watch app as well) that share functionality: perhaps they're the desktop and mobile versions of the same product, or perhaps they're two distinct apps which nonetheless rely on some algorithmic secret sauce you developed. Both projects are fairly large and complex, and undergoing active development, and there is a substantial amount of code shared between both apps, which is also undergoing development.

The simple approach would be to have the shared code in both apps' Xcode projects, separately. Of course, the problem with this is that the common code is in two distinct codebases, both of which are being developed. Unless special effort is taken to keep it synchronised, it will diverge, and the technical debt you will end up with will be two increasingly different versions of code which does (mostly) the same thing, which would then take increasingly more effort to unify into one body of code.

A better approach would be to take the common code (making sure, first of all, that it is nicely modular and loosely coupled to whatever uses it) and move it to a separate codebase, where it can be maintained separately, and then to modify the apps which depend on it to import this codebase. There are several ways to do this: one could use a feature of the version control system, such as git submodules, or one could use an Xcode-specific dependency management system such as Cocoapods or Carthage. Here, I will be using Carthage; my choice of it is because it is more lightweight than Cocoapods (and does not require replacing your XCode Project with an encompassing Workspace under its management), and also higher-level than git submodules. In particular, this approach involves creating a Framework project, containing your shared code (and unit tests for it) and encapsulating it into a Framework, placing that in a git repository, and configure the projects for the apps that use this to import the framework using Carthage.

A cross-platform Framework

We need the Framework to be a cross-platform framework. For the sake of simplicity, we assume that the code for it is not going to contain any platform-specific code such as UI code (though if it does, it can be implemented with conditional compilation). As such, we create a macOS “Cocoa Framework“ project, which has two targets: a Framework target and a Tests target for unit tests. (Making it a macOS project means, for one, that the tests can run without launching the iOS Simulator.)

We add our code to the Framework target (structured into a hierarchy of groups as we see fit), and then add some tests to the testing target and run the tests to make sure that the code compiles and works. Once that's done, all we need to do is to make it cross-platform, and make it work properly with Carthage.

Making it cross-platform is relatively straightforward; firstly, we need to add the other targets to the build settings: we go to the Build Settings tab of the target, and change Supported Platforms. Xcode's interface for this is a single-choice drop-down menu, so we need to select “Other” and then add all the platforms we wish to support, including both simulator and device variants. The names for these devices are somewhat idiosyncratic (often having originated before the modern marketing name for the platform was settled), and are:

  • iphoneos and iphonesimulator for iOS.
  • watchos and watchsimulator for watchOS.
  • appletvos and appletvsimulator for tvOS.

The settings should look something like:

The framework project will also contain a header file, named typically name-of-framework.h, and declaring version number and string values. To make this cross-platform, we need to edit it slightly, replacing the line which reads:

#import <Cocoa/Cocoa.h>
with:
#include "TargetConditionals.h"

#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#else
#if TARGET_OS_OSX
#import <Cocoa/Cocoa.h>
#endif
#endif
(Adding other cases for tvOS and watchOS if needed.)


After this, we can try selecting an iOS device type from the target menu in the Xcode menu bar and building the framework for iOS; it should succeed, as it does for macOS.

Preparing it for Carthage

Next, we need to put our framework on Git in a form that is useful to Carthage. This is fairly straightforward, though there are a few things to keep in mind. The way Carthage works is as follows: when you run carthage update, it reads a list of Git repositories from the Cartfile, and for each one, checks it out to the Carthage/Checkouts directory, runs a build with xcodebuild, and puts the resulting Frameworks under Carthage/Build, organised by platform. From there onward, it is your responsibility to incorporate the built product into your Xcode project, as in the Carthage instructions.

The good news is that a git repository of an Xcode project that produces Frameworks is already almost in Carthage-ready form. There are only a few things that need to be attended to, which are:

  • As the Carthage documentation says, the scheme in the Framework project that builds the Framework needs to be shared. When the project is created, there is one scheme which is not shared. To share it, go to the Product | Scheme | Manage Schemes menu and check the “Shared” checkbox next to the scheme; like so:
  • Schemes are stored in separate files within the .xcodeproj directory which are, by default, not checked into git. You will need to add these with git add myproject.xcodeproj/xcshareddata/xcschemes/* and check them in.
  • Carthage uses git tags for versioning, which means that the repository containing the framework must have at least one tag consisting of a version number in the format v1.2. Make sure there is one and that it is pushed to the remote repository.
  • Keep in mind that Swift Frameworks have their own namespaces, with everything being private by default. What this means is that any functions, classes or similar in your code will not be visible to the app that imports your framework unless they've been marked public.
Additionally, you may wish to remove provisioning profile and code signing settings from the framework's project, as that will be taken care of when the framework's containing app is built. It's not required, but it makes the project neater.

Building the apps

Now that we have the a git repository containing our shared Framework, using it in either an iOS or macOS apps is straightforward; one just has to follow the Carthage instructions to add it to one's project.

best practices carthage howto ios macos modularity software engineering xcode 0

This will be the comment popup.
Post a reply
Display name:

Your comment:


Please enter the text in the image above here: