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
- Download the
SwiftMoment
library https://github.com/iamjono/SwiftMoment - Open Package.swift file.
- Go to the dependencies section.
- Add the path to where the
SwiftMoment
library is located locally in your machine via the.package(path: “/path/to/package”)
syntax. - Add the word
SwiftMoment
to dependencies under target.
- 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)
}
}
- 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
- Open Package.swift file
- Go to the dependencies section.
- 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.
- Alternatively, you can replace the
branch
field with thefrom
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)
.
- Add the word
SwiftMoment
to dependencies under target. - 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
- 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)
}
}
- 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
- Create the library
mkdir MySharedSwiftLibrary
cd MySharedSwiftLibrary
swift package init --type library
- 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
- Reference the library in your SCADE project:
a. Goto build file, dependencies section
b. Add entryMySharedSwiftLibrary
using the URL address
c. Add 1.0.0 to thefrom
attribute or alternatively add main to thebranch
attribute
c. AddMySharedSwiftLibrary
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
Updated about 1 year ago