Color Mode


    Language

Sharing code between iOS and Vapor

August 22, 2019

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.

swiftvaporiosdevelopment

Author

Emil Marashliev

Emil Marashliev

Senior Vapor & iOS Developer

In love with Swift, Vapor and CrossFit.

You may also like

September 8, 2023

Strengthening our AI knowledge on mobile apps

In today's digital age, mobile applications have become an integral part of our daily lives. From ordering food to managing finances, there seems to be an app for every need. Behind the scenes, these apps are powered by a technology that has revolutionize...

Dinakar MauryaWarren HarrodAntonin JolyEnric Macias Lopez

Dinakar Maurya, Warren Harrod, Antonin Joly, Enric Macias Lopez

iOSAndroid

September 6, 2023

How to create an Interactive Experience using the Spatial Creator Toolkit

Spatial is a metaverse platform that allows anyone to create immersive 3D spaces, which can be instantly shared to the Web, iOS, Android, and VR, and explored by others. The Spatial Creator Toolkit, powered by Unity, allows you to add even more interactiv...

Nairah Thaha

Nairah Thaha

Immersive Technologies

ServicesCasesAbout Us
CareersThought LeadershipContact
© 2022 Monstarlab
Information Security PolicyPrivacy PolicyTerms of Service