Skip to content

Create and Publish a Swift Package to GitHub

Last Updated 2025-10-05 UTC+8.

In the realm of iOS development, the process of creating libraries plays a crucial role in enhancing code reusability and efficiency. Leveraging GitHub as a distribution mechanism adds a layer of accessibility and organization to these libraries. This article delves into the steps involved in crafting a Swift Package, publishing it to GitHub, and guides you on how to import and update the Swift Package created.

Development Environment

  • Xcode 16.2
  • macOS

Create a New Project

  1. Go to GitHub, create a new repository with the Swift .gitignore template.

    Create GitHub repository

  2. Clone it, and cd to it from your terminal. E.g.,

    zsh
    cd demo-ios-module
  3. Initialize your Swift Package in Terminal:

    zsh
    swift package init
  4. Open your project in Xcode.

Set up APIs and Sample App

  1. Create a test class in your Swift Package. For example, in the Sources folder, create a new Swift file TestClass.swift:

    swift
    //
    //  TestClass.swift
    //  DemoLibrary
    //
    //  Created by Cinnie She on 5/10/2025.
    //
    
    public struct TestClass {
        public static func greet() -> String {
            return "Hello, Module Trial!"
        }
    }
  2. Create another Git project to setup the sample app.

    Create an App project

  3. Input project info, and create the project.

    Input App project info

  4. In the sample app we've just created, in Xcode, go to File > Add Package Dependencies.

  5. Click Add Local.

    Add local package

  6. Choose your Swift Package repo directory, which contains the Package.swift file.

  7. Add your Swift Package to your sample app target.

    Add project to sample app target

  8. You should now see the local package imported from the left panel in Xcode.

    Added Swift Package

  9. Now, you can test your library via your sample app locally. E.g., update your ContentView with:

    swift
    //
    //  ContentView.swift
    //  DemoSwiftModuleSampleApp
    //
    //  Created by Cinnie She on 5/10/2025.
    //
    
    import SwiftUI
    import DemoLibrary
    
    struct ContentView: View {
        var body: some View {
            VStack {
                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundStyle(.tint)
                Text(TestClass.greet()) // Update this line
            }
            .padding()
        }
    }
    
    #Preview {
        ContentView()
    }

Deploy to GitHub

  1. Specify the platforms and their minimum deployment targets that your Swift package supports by adding platforms in Package.swift file of your library directory. E.g.,

    swift
    // swift-tools-version: 6.0
    // The swift-tools-version declares the minimum version of Swift required to build this package.
    
    import PackageDescription
    
    let package = Package(
        name: "DemoLibrary",
        platforms: [
            .iOS(.v18) // Add this
        ],
        products: [
            // Products define the executables and libraries a package produces, making them visible to other packages.
            .library(
                name: "DemoLibrary",
                targets: ["DemoLibrary"]),
        ],
        targets: [
            // Targets are the basic building blocks of a package, defining a module or a test suite.
            // Targets can depend on other targets in this package and products from dependencies.
            .target(
                name: "DemoLibrary"),
            .testTarget(
                name: "DemoLibraryTests",
                dependencies: ["DemoLibrary"]
            ),
        ]
    )
  2. Git commit and tag your commit with your version number, e.g., 1.0.0. Push to GitHub. This should have created a GitHub Release automatically.

    Release swift package

  3. You can click the tag under the Releases section on the right panel of your GitHub repository page.

    Check GitHub Release details

Import the Swift Package

  1. In your another app project in Xcode, go to File > Add Package Dependencies and type in the GitHub link https://github.com/<your-github-username>/<your-swift-package-repo-name> to import the Swift Package.

    Import Swift Package

  2. Search your Swift Package using your GitHub repository's URL. You can obtain the URL from GitHub Clone > HTTPS.

    Search Swift Package

  3. If you haven't setup your GitHub account and personal access token in Xcode, it might prompt you to input your GitHub account name, and your personal access token. Create a personal access token in GitHub to access your Swift Package. (Go to Settings > Developer Settings > Personal access tokens > Tokens (classic), create a new token and give the neccessary permissions repo, admin:public_key, user, and write:discussion). After inputting the info in Xcode, you will be able to import your Swift Package.

    Personal Access Token Permissions

  4. Click Add Package.

  5. Select your app target to add the Swift Package dependency.

    Add Swift Package to Target

  6. You should now be able to import your library, and access the APIs in your Swift Package.

Update the Swift Package

To update your Swift Package within your app project, right click on the package in the left panel of Xcode, and click Update Package.