Creating an iOS Library the Right Way (SPM and CocoaPods)
As an iOS developer, libraries make our development process, therefore our life easier. Thanks to these libraries, we shorten our development process and make it practical. I’ll try to describe the issues we’ll face when developing our own iOS library(s).
In Short
We will develop a sample iOS library that you can use with Swift Package Manager (SPM) and CocoaPods.
While supporting SPM vs CocoaPods, there are two important differences that we need to know. First, file structure between them is different. Second, you publish them different. We will address both in the following steps as we develop.
Create SPM Project for our iOS Library
Open your XCode then create a new SPM library from path:
XCode > File > New Project > Multiplatform >Swift Package

Let’s examine the auto-generated files after creating your project.
README: It is the file that allows you to show your library how to used by other developers.
Package: It is the file where we define the basic settings of your SPM project. Settings such as Dependecy, test files are made here.
Sources Folder: It is the directory where we have swift files that we will develop.
Tests Folder: It is the directory where our unit tests are.
Edit Library Settings
Our library will support iOS (You can also support macOS in your own projects, if you want), it will not include testing and will support swift 5.0 above.
First, we will delete the .testTarget field in the targets segment in the Package. We are deleting the Test file because it will cause unnecessary complexity to include the files we do not use in our project.
To determine the minimum swift version, we set the swiftLanguageVersions key with .v5.
We determined the path where the development files of the project are located in Target.
As a last step, we provide our minimum iOS 11 support by adding .iOS(v11) to the platforms key to determine the platform on which our library will run.
In the final, your Package file will look like this:
[gist id=”bdac4d1b67f7f4296be55cf402ab6723″ /]
Development Time
Now it’s time to make our library functional. This library will enable us to turn the flash of our phone on and off with a single function.
After deleting our default swift file under AppCircleSampleProject, we add our own swift file.
[gist id=”4db0f6ebf97e2c97550f81dd81a9992c” /]
It enables the classes that provide the FlashUsable protocol to turn the flash on and off if the flash is accessible.
The reason why we use #if os(iOS) at the beginning of our Swift file is that our SPM package can support other platforms, but UIKit is only used on the iOS platform. In order to eliminate this error.
Our last step is to share our SPM library as a public repo on GitHub. Now our SPM package is ready to use. My sample repo link that I have prepared for you => https://github.com/ferhanakkan/AppCircleSampleProject
Adding CocoaPods Support to our iOS Library
After providing our SPM support, it’s time to give our CocoaPods support. For this, we will first create our CocoaPods project in a different file directory.
If CocoaPods is not installed on your computer, install it with the command.
sudo gem install cocoapods
After the installation process is complete, create your CocoaPods project in the directory you choose with the following command line. While creating your project, you will receive certain questions about the project details and you can answer them as follows.
pod lib create ProjectName
[gist id=”cc2e137e7ab13cff507a4355776a3618″ /]
There are 3 files we need to get from the Cocoapods project we have created, these are:
- ProjectName.podspec
- Example folder
- LICENSE
We need to move these 3 files to the file where our SPM package is.
You can delete the remaining CocoaPods project files. Now is the time to let CocoaPods side see the our developments on the SPM side. For this, we need to open our Podspec file via XCode.
[gist id=”b4b7c4358628b9799ef7ee548df1da04″ /]
We are now making the arrangements we have made in the Package file for SPM settings via the Podspec file for CocoaPods. While publishing your library on the CocoaPods side, validation is done by looking at your Podspec file. The most important change we have made here is that we changed the path of s.source_files so that it goes to the file directory where the developments are. Now both SPM and CocoaPods packages use common development files. All files under the Sources/AppCircleSampleProject folder are considered development files.
Now we have completed our Cocoapods support. What remains after that is some polishing and publishing our library.
Polishing
What is expected by the users when they look at the libraries is to easily understand the README without examining the source code inside. So feel free to spend your time on your README files. The next step as you know is to edit the sample project that comes with CocoaPods to show how your library should be used. As another important note if your library serves the UI side, you should not forget that you need to provide both programmatic and Xib support.
I will have the README file and the sample project in our repo that I have created, you can take it as an example from there.
In order to access your library in your sample project, you may need to enter the example in your project file and change the path in the Podfile depending on the version of CocoaPods you are using. The reason we do this is because we change the directory of our project’s development files.
[gist id =”a6d0a6e2e42520fe99725e57443a071a” /]
Publishing Our iOS Library
After this exhausting process, there are only a few steps left to share our iOS library. On the SPM side, just post your code to your GitHub repo. Developers will be able to access your library when they enter your SPM link via XCode. But for the CocoaPods side, we need to do a few more actions.
As the first step, we enter our GitHub repo and click on tags. We create a New Release from here and enter the title according to the version number in our Podspec file and commit. After making the create release, the GitHub part is over, it’s time in the terminal.
After coming to the project location via terminal, save your e-mail address with the command line. You will receive a confirmation mail.
pod trunk register 'your e-mail address'
After confirming your mail, it’s time to see if our project is suitable for publishing. I recommend you to edit the warnings, either you gave a path error or an unreachable link. Using “ — allow-warnings” will cause you to encounter a possible error in the future.
pod lib lint
If you are ready to publish your validated CocoaPods library, run the command line and your library is now available to all developers.
pod trunk push
[gist id=”627f914a8f4494d35ae8059e3de1863f” /]
My sample repo => https://github.com/ferhanakkan/AppCircleSampleProject
Congratulations! Your iOS library has now been published to both SPM and CocoaPods.



