Skip to content

How to create a new Swift Library

husnjak edited this page May 23, 2023 · 2 revisions

Swift Package Manager

Create a new project in XCode File -> New -> Swift Package.
This will set up a new Swift Package project for you. The project should have a Package.swift file, Sources, and Test folders.

SwiftPackageName
  ├── README.md
  ├── Package.swift
  ├── Sources
  │   └── SwiftPackageName
  │     └──SwiftPackageName.swift
  └── Tests
      |── SwiftPackageNameTests
      | |──SwiftPackageNameTests.swift
      | └──XCTestManifest.swift
      └── LinuxMain.swift

Add all of your files under the folder /Sources and /Tests then go to Package.swift

This is the file that will tell Swift Package Manager the platforms you support, the dependencies you need to build your package, and the Swift version to compile, among others.

import PackageDescription

let package = Package(
    name: "SwiftPackageName",
    platforms: [
        .macOS(.v10_15),
        .iOS(.v12),
        .watchOS(.v6)
    ],
    products: [
        .library(
            name: "SwiftPackageName",
            targets: ["SwiftPackageName"]),
    ],
    dependencies: [
        .package(url: "https://github.com/DependencyOne.git",  from: "0.1.0"),
        .package(url: "https://github.com/DependencyTwo.git", from: "1.0.0")
    ],
    targets: [
        .target(
            name: "SwiftPackageName",
            dependencies: ["DependencyOne", "DependencyTwo"]),
        .testTarget(
            name: "SwiftPackageNameTests",
            dependencies: ["SwiftPackageName"]),
    ],
    swiftLanguageVersions: [.v5]
)

Note: The dependencies specified here must support Swift Package Manager.
That’s it, you have a Swift package ready to be referenced! You just need to create a new tag in your repo and specify a version number.

CocoaPods

Go to your repo root folder and create a file with the extension .podspec. The file name should normally be your package name, but it’s not necessary. Inside this file, add the following:

Pod::Spec.new do |s|
  s.name                  = 'SwiftPackageName'
  s.version               = '0.1.0'
  s.summary               = 'Brief summary'
  s.description           = "Package description"
  s.documentation_url     = "https://SwiftPackageName-docs.com"
  s.homepage              = 'https://github.com/husnjak/SwiftPackageName'
  s.license               = { :type => 'MIT', :file => 'LICENSE' } # Requires a LICENSE file in the root.
  s.author                = { 'Author' => '[email protected]' }
  s.source                = { :git => 'https://github.com/husnjak/SwiftPackageName.git', :tag => s.version.to_s }
  s.platforms             = { :ios => "12.0", :osx => "10.15", :watchos => "6.0" }
  s.ios.deployment_target = '12.0'
  s.swift_version         = '5.0'
  s.source_files          = 'Sources/SwiftPackageName/**/*.swift'

  s.dependency 'DependencyOne', '~> 1.0.0'
  s.dependency 'DependencyTwo', '~> 0.1.0'
end

CocoaPods is a central repository so it's important that you’ve created a tag and that s.source correctly references to.
To upload your framework to CocoaPods, go to your terminal and fun this command in the project root, where the .podspec file is located.

pod trunk register [email protected]

Verify your email, if it's a new email, and then:

pod trunk push SwiftPackageName.podspec

If it fails you can debug your podspec file using pod spec lint SwiftPackageName.podspec --verbose

Clone this wiki locally