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 4.0 standard. It should

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

Specify dependencies in the build file

We added a section in the build file to store the dependencies.

Every dependency will show in the project explorer:

Adding your first SPM library - SwiftMoments

Please open the build file and goto the dependency section

  1. .build right click - open with Text Editor
  2. Goto the dependency section.
  3. We want to add the SwiftMoment library from https://github.com/iamjono/SwiftMoment
  4. Add the url in the url field
  5. Add the tag that identifies the version (here 1.1.0) to the from from field
  6. Add the word SwiftMoment to linkedLibs
  1. 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 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 master branch
git checkout master
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 adress
    c. Add 1.0.0 to the from 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