CocoaPods对Swift程序的支持

When we work on a real project,we use many third-party libraries to make our lives easier. To use these libs,we first need to find them,download them and include them in our project.CocoaPodslets us automate all this process and also handles dependency management for us. Check out thispost on NSHipsterfor a very good intro.

CocoaPods is likeRubyGemsfrom the Ruby world andNPMfrom the Node.js world.

Using Cocoapods is really easy. You open terminal and change to your project directory (or just drag your project directory onto Terminal icon). You install CocoaPods by runningsudo gem install cocoapodsin Terminal. Then you need to create aPodfilein your project directory. APodfilefor including very popularAFNetworkinglib will look like this:

source 'https://github.com/CocoaPods/Specs.git' platform :ios, '7.0' pod 'AFNetworking',152)!important">'~> 2.4' 

And you are all set to use CocoaPods. You runpod installand you get an Xcode workspace file.xcworkspacecreated alongside your project file. From now on you need to always open and build project using workspace file instead of project file and you will be fine.


Update 11th March 2015:This blog post is outdated now asCocoaPods 0.36,which supports Swift,has been released and you just need to dosudo gem install cocoapodsto install it. You can read complete details about what all is different in 0.36 on CocoaPods officialblog. Also don’t forget to putuse_frameworks!before thepodstatements to support dynamic frameworks in your project.


When Swift became available,iOS programmers wanted to use their much beloved CocoaPods to integrated with already existing ObjectiveC libraries and that was not really difficult,above steps work with Swift projects as well,you just needa couple more stepsand you are done.

However soon some new and shiny libraries written in Swift came on the scene. My favorites beingAlamofireandSwiftyJSON,but CocoaPods was not yet ready for Swift libs and people had to rely on usingalternative methodsto use these libs. HoweverCocoaPods 0.36is just around the corner and it will include support for Swift Pods.


Update 25th Dec. 2014:CocoaPods decided to spread some Christmas cheer by making0.36 Beta availablefor everybody. You now just need to dosudo gem install cocoapods --preto install the Beta and not bother with theGemfileandbundle installsteps given below. They are outdated and won’t work anylonger. However you may still need to use custom branch/fork in yourPodfileas given in 3rd step and then runpod installto begin using Swift Libs with your project.


While a public release is still some time away,you can use CocoaPods unreleased code to start integrating Swift libs in your projects right now. Just be cautIoUs,that this is not fully tested code and treat it as a stopgap measure till 0.36 comes out. Ok so let’s see what needs to be done:

  • Create a file,calledGemfilein your project directory and just paste these lines in the file and save:
'https://rubygems.org'  gem 'cocoapods', :git => 'https://github.com/CocoaPods/CocoaPods.git',115)">:branch => 'swift' 'cocoapods-core',152)!important">'https://github.com/CocoaPods/Core.git' 'xcodeproj',152)!important">'https://github.com/CocoaPods/Xcodeproj.git' 'claide',152)!important">'https://github.com/CocoaPods/CLAide.git' 

  • Run commandbundle install
  • Create aPodfile. If you want to use Alamofire and SwiftyJSON in your project,yourPodfile1should look like this:
'https://github.com/CocoaPods/Specs.git'  '8.0' 'SwiftyJSON',115)">:git => "https://github.com/orta/SwiftyJSON",115)">:branch => "podspec" 'Alamofire',152)!important">"https://github.com/mrackwitz/Alamofire.git",152)!important">"podspec" 

  • Runbundle exec pod install2and you are done. You get the workspace file which you need to use from now on.

Now you should be able to import Alamofire and SwiftyJSON in your project and use3them.

Hope you liked this post. Connect viaTwitter&emailfor future updates. Thanks for reading.


  1. @H_301_237@ Each lib should have apodspecfile to work with CocoaPods,since that’s not the case right now,we are using forks of libs which contain a podspec file. This also means that if you want to use any other Swift lib with CocoaPods,you will need to fork it and create a podspec file for it yourself and then you will need to use path of your fork in the podfile.

  2. @H_301_237@ ‘bundle exec’ ensures that you are using Swift CocoaPods version from your Gemfile. ThanksBrianofQuickfor the tip.

  3. ThanksAmbasfor the tip about CLAide.

Nov21st,20146:38 pmalamofire,cocoapods,code,swiftyjson

相关文章

Swift 正式开源!Swift 团队很高兴宣布 Swift 开始开源新篇章。自从苹果发布 Swfit 编程语言,就成为了...
快,快,快!动动您的小手,分享给更多朋友! 苹果去年推出了全新的编程语言Swift,试图让iOS开发更简单...
开发者(KaiFaX) 面向开发者、程序员的专业平台! 和今年年初承诺的一样,苹果贴出了Swift语言的源码,...
本文由@Chun发表于Chun Tips :http://chun.tips/blog/2014/12/11/shi-yong-swift-gou-jian-zi-ding-yi...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...