CocoaPods are the de facto way of sharing and distributing iOS and OS X code. It manages third-party dependencies in a easy, fast, and safe way. It’s much like NuGet for .NET, Bower for JavaScript, and npm for Node. There are thousands of CocoaPods available, but almost all of them are in Objective-C. Today I’d like to show you how to create CocoaPods in Swift.
CocoaPod 0.36 and Beyond!…
Version 0.36 marks a new milestone in CocoaPods. This was when CocoaPods announced official support for Swift. Now we will see a new wave of CocoaPods that have been created in Swift. So first thing’s first, update your CocoaPods to take advantage: gem install cocoapods
Dude, Where’s My Swift?!
Although CocoaPods officially supports Swift, many of the commands and templates associated with creating a new pod is only available in Objective-C (at the time of this writing at least). So we can’t use “pod lib create” to let CocoaPods automatically set up your pod structure. No Swift shortcuts yet.
Show Me the Steps!
We will have to create out CocoaPod the manual way for Swift. Here are steps to create your Swift pod:
- Create a GitHub repository that will store your CocoaPod. There are ways to do this with a private repo if needed, but let’s stick to open source. Once you create the repo, clone the repo to your local machine.
- Now create a Xcode workspace in the folder where you cloned your repo. You do this in Xcode by going to File > New > Workspace. This workspace will be used to house your pod source code, examples, tests, etc.
- Create a new PodSpec file by navigating to your workspace directory and typing the following command: pod spec create YOUR_POD_NAME. Open your new spec file and modify it according to your pod. Here is an example of one for a Swift pod:
Pod::Spec.new do |s| s.name = "MySwiftPod" s.version = "0.1" s.summary = "This is my amazing Swift CocoaPod!" s.description = <<-DESC This is my long description here... yada, yada. DESC s.homepage = "https://zamzam.io" # s.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif" s.license = { :type => "MIT", :file => "LICENSE" } s.author = { "Basem Emara" => "contact@zamzam.io" } s.social_media_url = "https://twitter.com/basememara" s.platform = :ios, "8.0" s.source = { :git => "https://github.com/basememara/cocoapods-swift-sample.git", :tag => s.version } s.source_files = "MySwiftPod/MySwiftPod/*.swift" end
All the instructions are in comments when you create the spec file, but hopefully my sample above helps. Notice the last line for “s.source_files” leads somewhere, but I will get to that in a minute.
- Add a new iOS project which will be used to test our CocoaPod. To do this in Xcode, right-click on the left panel in the workspace and select “New Project”. Choose a “Single View Application” under iOS > Application section, or any project type you like. Be sure to call it “Example” for consistency with other CocoaPods convention.
- Add a new Swift framework by right-clicking in the left-pane again and select “New Project”. This time, we will select iOS > Framework & Library > Cocoa Touch. After clicking next, it is really important to select the right name for your CococaPod, which will be used as a namespace for any project consuming this pod. Also, make sure it is added to the workspace, not the example project:
- Let’s create a sample Swift file to test things out later. In your new framework folder, add a Swift file called MySwiftPodManager or anything you like. Add a function to it to test out. Here’s how my file looks like:
import Foundation public class MySwiftPodManager: NSObject { public func getMyTest(value: String) -> String { return "Hello \(value)" } }
- To use this in your example project, you will have to add your framework as a dependency to your Example project. To do this, drag your Swift framework’s .xcodeproj file and add it the Example project root. Your file system in Xcode should look something like this:
We’re still not done adding the Swift framework as a dependency. Click on the root of your Example project and go to “Build Phases” then add your Swift framework to “Target Dependencies” and “Link Binary With Libraries”. When you click the “+” sign, you should see your Swift framework in the list (that’s why we had to copy the .xcodeproj file). Still in “Build Phases”, click on the + button at the top left of the panel and select “New Copy Files Phase”. Rename this new phase to “Copy Frameworks”, set the “Destination” to “Frameworks”, and add your Swift framework again here. Your entire “Build Phases” should look something like this:
To test out this works, go to your Example project’s ViewController and add the “import YOUR_FRAMEWORK_NAME” statement at the top and try using your new manager in the “viewDidLoad” for example. You may have to build a could times to kick the dependencies into gear, but after that if should not have any errors.
- Validate your PodSpec file by going into terminal and changing to your workspace, then typing: pod spec lint MySwiftPod.podspec. At this point, you will probably get this error: fatal: Remote branch 0.1 not found in upstream origin
- Commit all your changes to your git repository (I like using SourceTree for this). Once you have committed and pushed your changes, type this into the terminal to create the tag the pod spec validator is complaining about:
git tag -a 0.1 -m "Tagged version 0.1" git tag git push --tags
Now run the spec validator again and it should now pass validation.
- That’s it! It is now time to test out your new CocoaPod. You can do this before releasing it out into the wild. Make sure you have everything committed so the latest is in your repo. Close down your project and create a fresh new iOS project for testing. Create a new Pod file by going into Terminal and changing to its directory, then type: touch Podfile. In this file, add the following:
use_frameworks! pod 'MySwiftPod', :git => 'https://github.com/basememara/cocoapods-swift-sample.git'
Notice the “use_frameworks!” directive at the top. This is telling CocoaPods to enable Swift framework support. Next line is adding a pod to your project, but it is doing so from an arbitrary git repo, not the public CocoaPods directory. This allows you to test your pod without submitting it into the public directory. Save the file and go back to Terminal and type: pod install. This will install the CocoaPod to your project and create a workspace to use going forward. Close your project and open the workspace it created for you. Test that you have access to your CocoaPod by going to your project’s ViewController and add the “import YOUR_POD_NAME” statement at the top and try using the manager you created in your CocoaPod in the “viewDidLoad” for example. You may have to build a could times to kick the dependencies into gear, but after that if should not have any errors (sound familiar :).
- If everything worked as expected, it’s time to release your CocoaPod to the world. To do this, you need to register at CocoaPods and go through their steps in submitting it to them. Follow their getting started with trunk guide to deploy to the public.
Congratulations, you are now ready to share your Swift code between your projects and the world!
HAPPY CODING!!
Official documentation on creating CocoaPods in Swift is slim at this point. In addition, there are no CocoaPods templates for Swift yet either. Please leave your comments and feedback below to help improve and evolve this initiative.
UPDATE, JUNE 2, 2015: Swift and framework support added to version 0.37.2 using “pod lib create”. WOOHOO!! See docs for more details.
A really useful post thank you. Even though CocoaPods now supports the generation of Swift Dynamic (& straight source code drop in) projects the generated targets seem quite complicated and have many repeated uses of the words Example and Tests in them. Therefore creating a Swift Dynamic Pod using the steps above seems to lead to a for simpler structure. So this is still relevant.