How to Create a CocoaPod in Swift
In this tutorial, you’ll learn how to create a CocoaPod containing your Swift code, assets, and storyboard files. You’ll also learn all about Podspec files. By Keegan Rush.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Using Swift Static Libraries
Prior to CocoaPods 1.5.0, CocoaPods did not work with static libraries. If you depended on a pod that contains Swift, you had to integrate CocoaPods into your project via frameworks instead of static libraries by specifying use_frameworks!
in your Podfile.
This is no longer the case. As of CocoaPods 1.5.0, you can use CocoaPods with static libraries! This is a big deal, since not all libraries you might depend on are shipped as frameworks. Dynamic frameworks also come with a launch-time performance impact that some apps need to avoid.
By omitting use_frameworks!
from your Podfile, CocoaPods will integrate into your app using static libraries instead. Your Objective-C dependencies still need to work as a module, however. MBProgressHUD is an Objective-C dependency that does not support modules. Luckily, by adding :modular_headers => true
to the MBProgressHUD dependency in the Podfile, CocoaPods adds module support for us.
sudo gem install CocoaPods
Populating the New Pod
Save and close the Podfile, and then enter the following command in Terminal:
pod install
Just as you’d expect, this creates a workspace and installs the various requisite files.
Enter the following command in Terminal to open the newly created RWPickFlavor workspace:
open RWPickflavor.xcworkspace
Your Project navigator should now look like the following:
You now need to copy a few of the existing files from the IceCreamShop workspace into RWPickFlavor. Open IceCreamShop in Finder. You’ll find the following sub-folders:
- Categories
- Controllers
- Models
- Views
- Resources
Copy (don’t Move) them into the folder for RWPickFlavor, so that RWPickFlavor’s folder structure looks like this:
Next, open RWPickFlavor.xcworkspace and add the folders you copied to the project using the Project navigator by selecting File ▸ Add Files to “RWPickFlavor”… and choosing the five folders you just added. Command-click each folder after the first to add it to the selection. When you’re done, your Project navigator should look like the following:
Once you’re sure all the files have been copied over, delete the following groups from IceCreamShop.xcworkspace:
- Categories
- Controllers
- Models
- Views
Take care not to delete any of the following:
- AppDelegate.swift
- LaunchScreen.xib
- Anything under the Supporting Files group
- The Resources group
Next, back in the IceCreamShop workspace, open Info.plist, found under the Supporting Files group, and delete the line for Main storyboard file base name.
Are you wondering why you didn’t delete the Resources group? It contains the Images.xcassets asset catalog, which has the app icon and launch screen logo that you need. It doesn’t need the background image anymore, because that belongs in the RWPickFlavor pod. You can delete background from the IceCreamShop asset catalog.
Inside the RWPickFlavor workspace, delete the AppIcon and logo images from Images.xcassets. Now, your assets should be structured like this:
- AppIcon and logo inside IceCreamShop
- background inside RWPickFlavor
Also inside the RWPickFlavor xcworkspace, you need to make sure that the objects inside Main.storyboard are linked correctly. Find these three items inside the storyboard:
- Choose Your Flavor
- Ice Cream View
- Pick Flavor Data Source
Because you copied this storyboard from a different project, you need to ensure that the module for each of these items is correctly set to your new CocoaPod, RWPickFlavor.
Oftentimes, simply opening up the storyboard is enough to update it from its old module to the new one.
Now, back to IceCreamShop.xcworkspace. Build and run. You shouldn’t see any errors, and you’ll eventually see the “Ice Cream Shop” logo followed by a black screen.
Believe it or not, the hardest part of creating your pod is done!
Using CocoaPods and Git
Since CocoaPods is bootstrapped on top of Git, each pod will need to have its own Git repository. If you already have a preferred Git host, great — you can use it to host your repository.
If you don’t, GitHub is an excellent choice as it’s well-known by developers and has a free plan for open-source projects.
Bitbucket is another great option as it has a free unlimited tier, including private repositories, for teams of up to five developers.
This tutorial uses GitHub, but feel free to use your preferred Git host instead.
Setting Up Your GitHub Repo
First, Sign up or Login to your GitHub account.
Next, click on the + (create new) icon on the top right of the screen and select New repository as shown below:
Enter RWPickFlavor for the Repository name, and select Create repository.
GitHub will create a new repository under your account; you’ll then see the following screen with a Quick setup section that displays your repository URL:
You’ll need this URL in just a moment, so leave the page open for now.
Now you need a second repository to host all of your private pod specs — you’ll use this later on in the tutorial.
Open github.com in a new tab; again, press the Create new icon and select New repository. Name this repository RWPodSpecs. Tick Initialize this repository with a README. This adds a commit with a README to the new repository. CocoaPods requires your pod specs repo to have at least one commit or it won’t work, and this is an easy way to create that initial commit. Finally, select Create repository.
Leave this tab open as well so you can easily grab the URL later when you need it.
Setting Up the Podspec
Without a Podspec, RWPickFlavor is nothing more than a bunch of files. The Podspec is what defines an actual CocoaPod. The Podspec includes basic information such as the pod’s name, version and Git download URL.
You need to create the RWPickFlavor.podspec file for RWPickFlavor. Enter the following commands in Terminal, pressing Enter after each one:
cd ~/Documents/Libraries/RWPickFlavor
pod spec create RWPickFlavor
open -a Xcode RWPickFlavor.podspec
This creates RWPickFlavor.podspec and opens it in Xcode.
There’s a lot of excellent documentation and examples in the default Podspec. However, you don’t need most of it.
Replace everything in RWPickFlavor.podspec with the following:
Pod::Spec.new do |s|
# 1
s.platform = :ios
s.ios.deployment_target = '12.0'
s.name = "RWPickFlavor"
s.summary = "RWPickFlavor lets a user select an ice cream flavor."
s.requires_arc = true
# 2
s.version = "0.1.0"
# 3
s.license = { :type => "MIT", :file => "LICENSE" }
# 4 - Replace with your name and e-mail address
s.author = { "Keegan Rush" => "keeganrush@gmail.com" }
# 5 - Replace this URL with your own GitHub page's URL (from the address bar)
s.homepage = "https://github.com/TheCodedSelf/RWPickFlavor"
# 6 - Replace this URL with your own Git URL from "Quick Setup"
s.source = { :git => "https://github.com/TheCodedSelf/RWPickFlavor.git",
:tag => "#{s.version}" }
# 7
s.framework = "UIKit"
s.dependency 'Alamofire', '~> 4.7'
s.dependency 'MBProgressHUD', '~> 1.1.0'
# 8
s.source_files = "RWPickFlavor/**/*.{swift}"
# 9
s.resources = "RWPickFlavor/**/*.{png,jpeg,jpg,storyboard,xib,xcassets}"
# 10
s.swift_version = "4.2"
end
Just like a Podfile, the Podspec is written in Ruby. Be extra careful not to make any typos or else the pod will likely fail to validate or install later.
Here’s what’s going on:
- You first specify basic information about the pod.
- A Podspec is essentially a snapshot in time of your CocoaPod as denoted by a version number. When you update a pod, you’ll also need to update the Podspec’s version. All CocoaPods are highly encouraged to follow Semantic Versioning. If you’re not familiar with Semantic Versioning, see How to Use CocoaPods with Swift for more information.
- All pods must specify a license. If you don’t, CocoaPods will present a warning when you try to install the pod, and you won’t be able to upload it to CocoaPods trunk — the master specs repo.
- Here, you specify information about yourself, the pod author. Enter your name and email address instead of the placeholder text.
- Here, you need to specify the URL for your pod’s homepage. It’s OK to simply copy and paste the GitHub homepage from your browser’s address bar to use here.
- Replace this URL with the Git download URL from the “Quick Setup” section of the first repo you created above. In general, it’s best to use either a http: or https: URL to make it easier for other users to consume. You can use an SSH URL if you want, but you’ll need to make sure that everyone on your team — and whoever else needs access to the CocoaPod — already has their public/private key pairs set up with your Git host.
- Here, you specify the framework and any pod dependencies. CocoaPods will make sure that these dependencies are installed and usable by your app.
- Not all files in your repository will be installed when someone installs your pod. Here, you specify the public source files based on file extensions; in this case, you specify
.swift
as the extension. - Here, you specify the resources based on their file extensions.
- Finally, specify 4.2 as the version of Swift used in the pod.
Some of the information you entered is important for CocoaPods to know how to download and install your pod correctly. Other information, such as the license, summary and author information, are there to help users to learn about your pod, if you choose to make it public in the CocoaPods Master Specs Repo.