Creating iOS framework with CocoaPods, Carthage, SPM support and Travis for running test and Fastlane for release automation — Part 1

Orçun Deniz
5 min readMay 3, 2020

Creating Swift framework is easy but adding CocoaPods, Carthage and Swift Package Manager support at the same time can sounds like scary in the first place. In addition to that adding Travis to run test for all commits and adding Fastlane to automate release processes for all dependency managers looks like a horror movie. But don’t be afraid. After you finished this series, you can easily create your own Swift frameworks that uses all these tools to make it perfect!

This series consist of 5 parts:

Part 1 — Create CocoaPod and release it.

Part 2 — Add Carthage support

Part 3 — Add Swift Package Manager support

Part 4 — Integrate Travis to build example project and run tests for framework

Part 5 — Integrate Fastlane to automate release processes by running just one line of command.

  1. Install the CocoaPods by executing the following commands from the command line:
$ sudo gem install cocoapods

If you don’t have RubyGems to run gem command, you can download and install on RubyGems official page.

2. Run the following command in folder you want to create CocoaPod.

pod lib create ODCustomFramework

While above command running, it will ask you some questions to initialize CocoaPod:

What platform do you want to use?? [ iOS / macOS ]
> iOS
What language do you want to use?? [ Swift / ObjC ]
> Swift
Would you like to include a demo application with your library? [ Yes / No ]
> Yes
Which testing frameworks will you use? [ Quick / None ]
> None
Would you like to do view based testing? [ Yes / No ]
> No

ODCustomFramework successfully created. Here its file structure:

ODCustomFramework
├── .travis.yml
├── _Pods.xcproject
├── Example
│ ├── ODCustomFramework
│ ├── ODCustomFramework.xcodeproj
│ ├── ODCustomFramework.xcworkspace
│ ├── Podfile
│ ├── Podfile.lock
│ ├── Pods
│ └── Tests
├── LICENSE
├── ODCustomFramework.podspec
├── Pod
│ ├── Assets
│ └── Classes
│ └── RemoveMe.[swift/m]
└── README.md

Detailed explanation can be found at official CocoaPods guide.

3. Let’s push ODCustomFramework to GitHub. Obviously we have to create new repository on GitHub before pushing ODCustomFramework.

Create new repository and run following commands:

cd ODCustomFrameworkgit add .
git commit -m “Initial Commit"
git remote add origin https://github.com/orcundeniz/ODCustomFramework.git
git push -u origin master

Note: It’s better to change default old .gitignore file with GitHub’s new one for Swift. Go to project folder in Finder and press cmd + shift + dot to see hidden files, then open .gitignore and change with mine. Check git status to be sure that .gitignore is added. If it is not added, run git add .gitignore

4. You need to make some changes in project settings due to your needs. CocoaPods creates new pod with default settings such as iOS 9.3 as deployment target and Swift 4.0.

I want to make that ODCustomFramework supports at least Swift 5 and iOS 12. So you should change settings like I do in following pictures if you needed.

Pods -> Targets -> ODCustomFramework -> Deployment Info -> Target

Pods -> Targets -> ODCustomFramework -> Build Settings-> Swift Version

You should edit example project too.

ODCustomFramework -> Project -> ODCustomFramework -> iOS Deployment Target

ODCustomFramework -> Project -> ODCustomFramework -> Build Settings-> Swift Version

Open ViewController and import ODCustomFramework. Then build the ODCustomFramework-Example target to make sure everything is alright.

5. It’s time to edit podspec file to specify all informations for your pod.

A specification describes a version of Pod library. It includes details about where the source should be fetched from, what files to use, the build settings to apply, and other general metadata such as its name, version, and description.

Detailed information about podspec on CocoaPods.

After you’ve completed podspec, actually it’s time to implement your framework, tests and example project. Add files to the marked folders and don’t forget that you have to make classes, functions or properties public if you want to use them in Example project. However, you have to move these files in latter part of this series to support Swift Package Manager. You can implement your framework, tests and example project right now and move those later or skip this step for now and implement all of them when this series are completed.

NOTE: s.source_files = ‘ODCustomFramework/Classes/**/*’ line in podspec specifies that where the framework classes are located in. You have to locate new or existing files under this path. But this path is going to change latter parts of this series to support Swift Package Manager.

Whether you’ve done with implementing framework or not, you need to validate podspec to continue.

pod lib lint ODCustomFramework.podspec

6. Now that you have a fully functional pod running on your local machine. it’s time to make your framework available to others.

Step 1: Commit and push changes.

git add .
git commit -m “0.1.1 release commit"
git push

Step 2: Tag your git repo, then push

git tag 0.1.1
git push origin 0.1.1

You marked this commit as a specific release of your pod. The name of the tag should match s.version in your .podspec file.

Step 3: Validate the pod for release

pod spec lint ODCustomFramework.podspec

Step 4: Send your pod to Specs Repository for release!

pod trunk register deniz.orcun@outlook.com ‘OrcunDeniz’ — description=’iMac’//Check email and activate.pod trunk push ODCustomFramework.podspec

We’ve created and released ODCustomFramework on CocoaPods. Everyone can install ODCustomFramework by adding following lines to pod file:

pod 'ODCustomFramework', '~> 0.1'

However, ODCustomFramework can’t be installed via Carthage and Swift Package Manager yet. Besides that every time you want to release new version of your framework via CocoaPods, Carthage and Swift Package Manager, you have to go through following steps:

1- Implement changes for new version2- Run tests to be sure they are working3- Commit and push changes for new version.4- Tag new version to git
git tag 0.1.1
git push origin 0.1.1
Carthage and Swift Package Manager installs your framework from GitHub. If you configure your framework correctly, it can be installed via Carthage or Swift Package Manager after this step. However, CocoaPods has 4 more steps to complete release processes.5- Increment podspec version => i.e. s.version = '0.1.1'6- Validate local podspec:
pod lib lint ODCustomFramework.podspec
7- Validate pod for release
pod spec lint ODCustomFramework.podspec
8- Release
pod trunk push ODCustomFramework.podspec

To solve above problems and make ODCustomFramework perfect, follow other parts of the series:

Part 2 — Add Carthage support

--

--