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.

4.5 (23) · 2 Reviews

Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Creating the License

Like every other pod, you’ll need to create the LICENSE file.

Copy and paste the MIT License found here into your favorite text editor, and then save the file as LICENSE — with no extension — in ~/Documents/Libraries/RWPickFlavor. Make sure to replace [year] and [fullname] with the actual values — the year and your actual name, of course!

Choose a License is a really great site that helps you choose the most appropriate open-source license for your project, and it’s built and maintained by the good folks at GitHub.

Pushing to Git

You’re finally ready to push the RWPickFlavor pod to its new home on GitHub!

Enter the following commands in Terminal, replacing [Your RWPickFlavor Git URL] with the Git URL for RWPickFlavor repo you created earlier:

cd ~/Documents/Libraries/RWPickFlavor
git init
git add .
git commit -m "Initial commit"
git tag 0.1.0
git remote add origin [Your RWPickFlavor Git URL]
git push -u origin master --tags

If prompted, enter your username and password for GitHub.

This commits all of the files within the RWPickFlavor directory, creates a new 0.1.0 tag and pushes everything to your remote repository.

Congratulations, you’ve just created your first CocoaPod!

Congratulations!

You’ve created your first CocoaPod, but can you actually use it? Well, not quite yet.

Using Your New CocoaPod

You first need to add your Podspec to a private specs repo; this lets CocoaPods find the pod when you try to install it. Fortunately, you’ve already created a Git repo for this, so this final step is relatively straightforward.

Enter the following in Terminal, making sure you’re still in the RWPickFlavor directory:

pod repo add RWPodSpecs [Your RWPodSpecs Git URL]
pod repo push RWPodSpecs RWPickFlavor.podspec

Make sure to replace [Your RWPodSpecs Git URL] with the Git URL for RWPodSpecs repo you created earlier.

This creates a local reference to RWPodSpecs that’s stored in ~/.cocoapods on your machine, and pushes the RWPickFlavor.podspec to it.

You now have a private pod specs repo set up! Easier than you thought, right?

With that complete, it’s finally time to use your newly created pod.

Open the Podfile for IceCreamShop and replace its contents with the following:

platform :ios, '12.0'

source 'https://github.com/CocoaPods/Specs.git'
source '[Your RWPodSpecs Git URL Goes Here]'

target 'IceCreamShop' do
  pod 'RWPickFlavor', '~> 0.1.0'
  pod 'MBProgressHUD', '~> 1.1.0', :modular_headers => true
end

Make sure that you replace [Your RWPodSpecs Git URL Goes Here] with the Git URL for your RWPodSpecs repo. You don’t need to include Alamofire in the Podfile, because it’ll be pulled in as a dependency defined in RWPickFlavor.podspec. You still need a line for MBProgressHUD so that you can include the :modular_headers => true configuration.

Close IceCreamShop.xcworkspace. Then, run pod install in Terminal.

Finally, reopen IceCreamShop.xcworkspace and replace the entire contents of AppDelegate.swift with the following:

import UIKit
import RWPickFlavor

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  
  var window: UIWindow?
  
  func application(_ application: UIApplication, didFinishLaunchingWithOptions
    launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.rootViewController = createRootViewController()
    window?.makeKeyAndVisible()
    
    return true
  }
  
  func createRootViewController() -> UIViewController {
    let bundle = Bundle(for: PickFlavorViewController.self)
    let storyboard = UIStoryboard(name: "Main", bundle: bundle)
    return storyboard.instantiateInitialViewController() ?? UIViewController()
  }
}

In createRootViewController, you get a reference to the RWPickFlavor “bundle” — which is actually a dynamic framework — to create a reference to the Main storyboard and instantiate its root view controller.

Build and run. You’ll be greeted with the familiar “Choose Your Flavor” screen. Awesome!

choose_flavor

Note: CocoaPods has an issue with Xcode’s new build system that they should be fixing soon. If you do not see the background image show when you run the app, check the comments at the end for a workaround.

Where to Go From Here?

If you want to download the finished project, use the Download Materials button at the top or bottom of this tutorial.

You’re now ready to start making your own CocoaPods! However, what you’ve covered in this tutorial is really just the tip of the iceberg when it comes to CocoaPods. Check out the CocoaPods Guides to learn everything you need to know about creating CocoaPods.

After creating a CocoaPod, you might consider adding it to the CocoaPods Master Specs Repo so that it will be made available via CocoaPods.org to thousands of developers around the world. Refer to the CocoaPods Trunk blog post to find out how you can do this!

If you have any questions or comments about the tutorial, feel free to join the discussion in the comments below!