Code reusability has always been one of the ultimate goals for us developers. Building your backend with Vapor not only gives you highly scalable apps with shallow memory footprint, but also the potential to share some of your backend code with your iOS app without paying the extra cost. The downside comes when you try to share networking code between iOS and Vapor. This is due to the fact that Vapor is built on the top of SwiftNIO, which means you can not share your URLSession based code. SwiftNIO is Event-driven (based on Futures and promises) which is not compatible with URLSession‚Äôs callback approach. However, the good news is that Apple released NIO Transport Services - ‚ÄúExtensions for SwiftNIO to support Apple platforms as first-class citizens.‚ÄĚ In this post, I will outline some basic concepts about sharing entity objects between iOS and Vapor.

Create entity objects 

In the main repository, we will store the Swift package which holds the framework target with our codable entity objects:

swift package init

Add your entity objects under the Sources/MyFramework/ directory. They have to be public and conform to the Codable protocol, e.g.:

public struct SomeResponse: Codable {
  public let id: Int
  public let name: String
  public let isDefault: Bool

  enum CodingKeys: String, CodingKey {
    case id
    case name
    case isDefault = "is_default"

Create shim repository 

We need to create a Carthage shim repository (e.g.,¬†the idea is to have some ‚Äúglue‚ÄĚ between the main repository which is Swift Package Manager based and the iOS project. Of course, if your iOS project supports Swift Package Manager,¬†this is not necessary. In the newly created repository you need to add your main repository as a git submodule like so:

git submodule add

After that, in the root of your shim repository, you need to create a few symbolic links, for the Package.swift, Sources and Tests :

ln -s MyFramework/Package.swift Package.swift
ln -s MyFramework/Sources Sources
ln -s MyFramework/Tests Tests

Carthage requires shared .xcschemes, so we first need to generate .xcodeproj:

swift package generate-xcodeproj

Open the .xcodeproj in the Xcode, edit the schemes and ensure they are shared. Then git commit and git push the changes.

Add Carthage shim to your iOS app

Open Cartfile and add the Carthage shim, e.g.:

github "MyOrganization/MyFramework-shim" "master"

After that, run carthage update.

Add the package to your Vapor app

Open Package.swift and add next the line under dependencies:

let package = Package(
  name: "MyAppName",
  dependencies: [
  .package(url:", branch: "master")

Don’t forget to add MyFramework under target dependencies, e.g.:

 targets: [
    .target(name: "App", dependencies: [


With a few little tricks, we made our entity objects sharable between iOS and Vapor. However, this strategy could be utilized to share any functionality which does not depend on iOS SDKs.

swift vapor ios development


Emil Marashliev

Senior Vapor & iOS Developer

In love with Swift, Vapor and CrossFit.

You may also like

Building a COVID-19 Tracing app in a week

We wanted to create a prototype app to help people to find out if they contacted anybody infected by the COVID-19 disease. It will use wireless technologies searching near by and notify users. In this post we’re going to talk about the thoughts behind this app, why we made it...

Android Image Framework Comparison

Android Image Framework Comparison