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

Droidcon Berlin 2019

Droidcon Berlin 2019 Back in July, Lucas and I took a trip to Berlin for the Droidcon 2019 conference. It was the second mobile development conference that I’ve been to and the first one entirely dedicated to Android, so I had high hopes. In this blog I’d like to highlight...

Android
Using USDZ for a better AR experience

ARKit 3.0 was officially released together with iOS 13.0 and it includes a lot of augmented reality goodies, such as people occlusion and motion capture. You can read about all the new features here.

iOS