Overview

Everyone wants to share some piece of reusable code, or a set of functionality with others. We will take a look at a generic and simpler way of creating and sharing a Framework in Swift.

Support

It should support main packaging solutions like Swift Package Manager and Cocoapod. We will be keeping the structure in accordance with SPM.

Steps

  1. Create a Framework project.
  2. Once created, open the root directory and create a folder named ‘Sources’ and another called ‘Tests’. Move the folder named the target(in this case ‘Framework’), with an umbrella header file inside the Sources.
  3. Remove the existing ‘Framework’ folder reference in project, and add the ‘Sources’ and ‘Tests’ folders with ‘create folder reference’ into the project.
  4. Add your reusable source codes inside the ‘Sources’ with correct access control levels.

Now we have a framework project, which has some reusable component. Next step will be to add support for Swift Package Manager and Cocoapod.

Swift Package Manager

For the framework to be available in Swift Package Manager, we need to add a Package manifest file(Package.swift). This will have information about the Framework, its dependencies, targets, etc.

To achieve it, run swift package init, from project root directory. It will create manifest, README and gitignore files.

~/Documents/projects/Framework 6:11$ swift package init
Creating library package: Framework
Creating Package.swift
Creating README.md
Creating .gitignore

Run swift build, to make sure the project is runnable.

If in case, you didn’t create unit test with project creation, it will show error saying:

error: 'framework': Source files for target FrameworkTests should be located under 'Tests/FrameworkTests', or a custom sources path can be set with the 'path' property in Package.swift

For this, go inside the framework project and just add a unit test bundle target. If created from project, it will be created inside the root directory, then move to ‘Tests’ folder.

Now run swift build, and make sure build is complete.

~/Documents/projects/Framework 6:19$ swift build
Building for debugging...
[2/2] Compiling Framework Reusable.swift
Build complete! (0.56s)

Sharing SPM

For sharing the Swift Package Manager, we will be tagging the git version with the manifest file and push the tag to origin. For development purpose, drag and drop the package to the Xcode project/workspace(editing local package dependency)

Cocoapod

For initiating the a podspec file, run pod spec create <FrameworkName>. Update the podspec with relevant information.

Pod::Spec.new do |spec|

  spec.name         = "Framework"
  spec.version      = "0.0.1"
  spec.summary      = "A short description of Framework."
  spec.description  = <<-DESC This is a sample description for the Cococpod library. 
                   DESC

  spec.homepage     = "http://github.com/bob/Framework"
  spec.license      = "MIT"
  spec.author             = { "bob" => "bob@email.com" }
  spec.platform     = :ios
  spec.platform     = :ios, "15.0"

  spec.source       = { :git => "http://github/bob/Framework.git", :tag => "#{spec.version}" }


  spec.source_files  = "Sources/Framework/*.swift"
  spec.resources = "Sources/Framework/Resources/*.xcassets"

  spec.dependency "JSONKit", "~> 1.4"

end

Once done, use another project and point to the local podspec and update the pod(developing pod).

Posted in

One response to “Sharing a Framework”

Leave a comment