加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

CocoaPods对Swift程序的支持

发布时间:2020-12-14 01:53:56 所属栏目:百科 来源:网络整理
导读: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 handl

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. 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. ‘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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读