Swift Package Manager(SPM) Part 2


In this blog we will see how we can create our own swift package and published it as private and how can we use it on some other project

Create A Swift Package

Packages are a great way to share code both in your workspace with your team or with a wider open source community

As shown in Figure 1 Go to File → New → Swift Package → Home

Figure 1

As shown in Figure 2 we created Home package (module). Before we go further let’s do some theory parts

Figure 2


  1. A package is a directory that contains a Swift Package manifest file. As shown in Figure 2 Package.swift is a manifest file
  2. The manifest is a file called Package.swift and it identifies that directory is a Swift Package
  3. It also contains the Source and Tests directory
  4. Underneath the source directory, there are subdirectories for each of the separate targets in the package. These are the separately buildable component of the package and similarly, under the Test, there is a separate subdirectory for every test suite. Currently our Home package only have one target and one library which is also called Products. If You don’t get point 4, it’s OK I will refer to this point in the future as well when we work on a complex manifest file

Manifest File

  1. // swift-tools-version:5.3 , the first line in the manifest file. This is the minimum version of the Swift compiler that is required to build your package
  2. After the first line, we define package configuration using package description API
  3. The package description API is a declarative API that is provided by the package manager package description library and by importing that then the rest of the contents of the file can declare the characteristics of the package
  4. By using the Package constructor we first define the name of the package, products then targets. We will explore more this later when we will be playing with manifest file
  5. products → This section lists product package vends to the client, So the package can control which parts of your code can be directory imported by the client and in this case, we only have one library with the same name as package called “Home”. In short, library publishes the Home target to the client as a library
  6. Target → list individual buildable/executable parts of the package
  7. Test target are under directory called Tests ad they also have their own subdirectory
  8. They are declared using the test target API and since test target are usually testing another Target , so you need to provide dependency on the target that it is testing
  9. Note: Test target will not actually be linked to the client

As shown in Figure 3, we write some unit test and added some code

Figure 3

Publish Swift Package

As shown in Figure 4, I just created a private repo in github repo

Figure 4.1
Figure 4.2

Open Terminal ,

  1. Change the current working directory to your local project
  2. run git init
Figure 5


  1. git add .
  2. git commit -m “First commit”
  3. git remote add origin git@github-aliakhtar49:aliakhtar49/Home.git (At the top of your GitHub repository’s Quick Setup page, click to copy the remote repository URL., since I am using mulitple ssh I used github-aliakhtar49)
  4. git push -u origin master
Figure 6
Figure 7

Now tap on create new release

Figure 8

Then public release

Figure 9

As shown in figure 10 we published a release with tag 1.0.0

Figure 10

Used Published Swift Package

File → New → Swift Packages → Add Package Dependency

Figure 11

Select Main App target

Figure 12

Choose Package repo

As shown in Figure 13 Xcode are unable to fetch , since it’s a private repo we need to add github account in Xcode as well

Figure 13

Go to Xcode → Preference → Account → + → Select Github → Continue

Figure 14

As shown in Figure 15, we need to generate access token for that

Figure 15

Go to github account → Setting → Developer settings → Personal access tokens. → Generate token → Make sure to copy your new personal access token now. You won’t be able to see it again!

Figure 16

As shown in Figure 17, I configured Xcode with ssh and point it to the right ssh file

Figure 17

As shown in Figure 18 , Xcode now successfully fetch repo through ssh you can use https as well but I prefer ssh , we need to configure github on Xcode since repo is private

Figure 18
Figure 19

As shown in Figure 20 we successfully imported our Home module

Figure 20

As shown in Figure 20 , we imported package since Xcode fetch package from repo and linked automatically package to our project, so in next section we will discuss how Xcode linked packages

Linking Package Libraries

  1. Your project contain source files and you package also contains source files under directory Source
  2. What Xcode does it takes all of these source files and it compiles them and particularly compiles the package code in a way that is compatible with the app code in your project. These includes architecture , platform etc
  3. It recompile multiple times, if needed depending on what your app needs
  4. Then it links it in and combines all of that into the application
  5. Package library are static by default and so all the code is linked together and this is repeated for various apps in your project that use the same package Like if you have an iOS app and watch OS app, they use the same package. Xcode might build the code multiple times as needed for each of those apps


In the next part we will see how we can edit package by locally adding in our project

Useful Links


Senior iOS Engineer | HungerStation | Delivery Hero