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. https://github.com/MyOrganization/MyFramework-shim), 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 https://github.com/MyOrganization/MyFramework

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: https://github.com/MyOrganization/MyFramework.git", branch: "master")
  ]
)

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

 targets: [
    .target(name: "App", dependencies: [
  "MyFramework",
]);

Conclusion 

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

Author

Emil Marashliev

Senior Vapor & iOS Developer

In love with Swift, Vapor and CrossFit.

You may also like

An overview of watchOS complications

Complications are bits of watchOS application that can be put on the user’s watch face and offer quick information, simply by looking at his/her Apple Watch, without opening the accompanying watchOS/iOS app. The Apple Watch ships with some default complications: Calendar, Weather, Activity, Battery (it shows only the watch’s battery...

iOS
Nordic.js 2019

The 9th version of the Nordic.js conference took place at Magasin 9 near the harbor of Stockholm. The whole frontend team was invited, and three of us were able to attend (Rasmus, Themi and Jonas). It was our first trip to a conference as a team, and it was nice...

frontend afk