Distribute your Swift code using CocoaPods

Haroon Baig
3 min readFeb 19, 2015

--

It was the most exciting day for me when Apple announced Swift in its WWDC 2K14 event. Suddenly, it created a hype around the iDevelopers community, and a lot of them started porting their libraries and frameworks to Swift.
CocoaPods is, and will remain the de-facto standard for distributing libraries and frameworks. The beauty of it is that its centralised, which means you can easily trace the version numbers and releases without any hassle. But, the problem was, that the existing version of CocoaPods didn’t support Swift based libraries and frameworks (at the time I’m writing this article). But now, the wait it over. CocoaPods 0.36 (pre-release version) comes with the support for Frameworks and Swift. Lovely!

What you have to do ?

Well, you have to create a .podspec file into the root of your Xcode project. Thats it! Lets take an example of an OpenSourced Swift based CoreMotion wrapper called MotionKit. I made this wrapper in pure Swift and it gained a lot of traction in the iOS Dev community. If we look closely at the directory structure of it, we will find a file named ‘MotionKit.podspec’. That’s the file where all the magic is, and you only have to add this file to the root of your Xcode project, you’d be ready to distribute your code then. Lets dig deeper into whats in there.

Also, make sure that you are using the pre-release version of the CocoaPods. You can grab it with just following command.

[sudo] gem install cocoapods --pre

The .podspec file — With all its Glory

Have a look at the sample .podspec file taken from the famous iOS CoreMotion wrapper, The MotionKit.

Pod::Spec.new do |s|
s.name = 'MotionKit'
s.version = '0.6'
s.license = 'MIT'
s.summary = 'CoreMotion Made insanely simple'
s.homepage = 'https://github.com/MHaroonBaig/MotionKit'
s.social_media_url = 'https://twitter.com/PyBaig'
s.authors = { 'Haroon Baig' => 'haroon.prog@gmail.com' }
s.source = { :git => 'https://github.com/MHaroonBaig/MotionKit.git', :tag => s.version }

s.ios.deployment_target = '8.0'

s.source_files = 'MotionKit/*.swift'

s.requires_arc = true
end

Note that the podspec file uses the ruby syntax. Lets go through each of the fields because this is the only thing that you have to understand.

  • s.name is the name of your Pod
  • s.version is the version number (important)
  • s.source is the the link to where the source files of your Code/Framework are.
  • s.source_files tells the podspec file to look for the source files specifically under the MotionKit directory, with the extension of *.swift

Thats all you need. Just push that file to the root of your repository (if you’re using GitHub), or the root of your Xcode project.

Now Test your newly created Pod

Now is the time to test your pod, to make sure it works fine. Quickly create the pod file into your test project and add the following line to it.

pod '[s.name]', :git => '[s.source]'

Replace the ‘[s.name]’ with the s.name of your podspec file, and ‘[s.source]’ with the s.source that you entered into your podspec file. In the perspective of the example podspec file presented above, it should probably look like this.

pod 'MotionKit', :git => 'https://github.com/MHaroonBaig/MotionKit.git'

Now run the pod install command from your terminal and see if everything works fine. If the Pod worked out correctly, it’d the be the perfect time for you to take the ownership of your Pod and publish it with the new Trunk Service released with the 0.36 version of CocoaPods.

Publish your Pod—The new Trunk Service

Here, you just have to do two things. First, register yourself and second, publish your Pod. All it takes is just a few commands and you’re ready to go.

Registering yourself

$ pod trunk register haroon.prog@gmail.com "Haroon Baig"

Publishing your Pod

$ pod trunk push [s.name].podspec

Replace the ‘[s.name]’ with the one you had entered in your podspec file.

Having that done, you can now distribute your pod like this.

pod '[s.name]', '~> [s.version]'

Replace the s.name and s.version with the ones that you entered in your podspec file. Happy Coding.

--

--

Haroon Baig

👋 I help companies in the crypto space produce better, insightful, & engaging content that people LOVE! Everything from ECDSA to zk-SNARKs, Bitcoin to Polkadot