Swift Package Manager Guide

Use SPM for managing packages for cross platform Swift development on Android and iOS

Introduction

SPM is the default package manager for Apple Swift. Apple will focus on SPM as its current and future standard for the distribution of Swift code. Its main goal is the automation of downloading, compiling and linking Swift code. For a larger discussion, see here https://swift.org/package-manager/

We at SCADE use SPM to accelerate mobile app development by enabling the incorporation of libraries into the app:

  • Reference and link your custom written library
  • Reference and link other popular 3rd party libraries
  • Reference and link other popular 3rd party libraries that wrap C libraries

We support packages version 4 and higher.

Package.swift file

Please make sure that your package swift file conforms to the Swift 5.3 standard. It should

  • start with the // swift-tools-version:5.3 entry
  • contain targets and products
  • all source code files should be in ./Sources// directory

Specify dependencies in the Package.swift file

There is a section in the Package.swift file to store the dependencies.

Ways of using SPM in SCADE

There are two ways that Swift packages can be used in SCADE. They can be used locally in a project or via the URL of a Git repository.
The SwiftMoments library from https://github.com/iamjono/SwiftMoment will be used as an example for this guide.

Adding SPM library Locally

Please open the Package.swift file and goto the dependencies section

  1. Download the SwiftMoment library https://github.com/iamjono/SwiftMoment
  2. Open Package.swift file.
  3. Go to the dependencies section.
  4. Add the path to where the SwiftMoment library is located locally in your machine via the .package(path: “/path/to/package”) syntax.
  5. Add the word SwiftMoment to dependencies under target.
  6. Use SwiftMoment
import ScadeKit
import SwiftMoment

class MainPageAdapter: SCDLatticePageAdapter {

	// page adapter initialization
	override func load(_ path: String) {		
		super.load(path)
				
		// test SwiftMoment
		let now = moment()
		print(now)
		
	}
}
  1. Success. You are now using Swift SPM libraries to build iOS and Android applications

Adding SPM library via Git Repository URL

Please open the Package.swift file and goto the dependencies section

  1. Open Package.swift file
  2. Go to the dependencies section.
  3. Enter the SwiftMoment library details inside .package(name: String, url: String, .branch("")) syntax.
    i. Add the name in the name field.
    ii. Add url in the url field.
  4. Alternatively, you can replace the branch field with the from field that takes in a tag that represent a particular version of the repo being used. For instance, .package(name: String, url: String, from: Version).
  5. Add the word SwiftMoment to dependencies under target.
  6. You can now use the code from the library
    You might receive an indication from the IDE that import SwiftMoment cannot be found. You can ignore this, the error will go away after first compilation.

Ignore this initial warning. It will go away after compilation

  1. Use SwiftMoment
import ScadeKit
import SwiftMoment

class MainPageAdapter: SCDLatticePageAdapter {

	// page adapter initialization
	override func load(_ path: String) {		
		super.load(path)
				
		// test SwiftMoment
		let now = moment()
		print(now)
		
	}
}
  1. Success. You are now using Swift SPM libraries to build iOS and Android applications

Creating your own SPM library

One of the easiest ways of reusing code between a SCADE project and an iOS project is the use of a SPM Library

  1. Create the library
mkdir MySharedSwiftLibrary
cd MySharedSwiftLibrary
swift package init --type library
  1. Upload to GitHub
    a. make sure you tag the current master version
// after uploading to github, tag your main branch
git checkout main
git tag 1.0.0
git push --tags
  1. Reference the library in your SCADE project:
    a. Goto build file, dependencies section
    b. Add entry MySharedSwiftLibrary using the URL address
    c. Add 1.0.0 to the from attribute or alternatively add main to the branch attribute
    c. Add MySharedSwiftLibrary to linkedLibs

Upcoming features

  • IDE Wizard to build SPM Library project from IDE (use command line for now)
  • Hierarchical code dependencies support where library X depends on library Y