CocoaPods对Swift程序的支持

来源:互联网 发布:node.js sqllite 编辑:程序博客网 时间:2024/05/18 00:14

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. CocoaPods lets us automate all this process and also handles dependency management for us. Check out this post on NSHipster for a very good intro.

CocoaPods is like RubyGems from the Ruby world and NPM from 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 running sudo gem install cocoapods in Terminal. Then you need to create a Podfile in your project directory. APodfile for including very popular AFNetworking lib will look like this:

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

And you are all set to use CocoaPods. You run pod install and you get an Xcode workspace file .xcworkspace created 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 as CocoaPods 0.36, which supports Swift, has been released and you just need to do sudo gem install cocoapods to install it. You can read complete details about what all is different in 0.36 on CocoaPods official blog. Also don’t forget to put use_frameworks! before the pod statements 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 need a couple more steps and you are done.

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


Update 25th Dec. 2014: CocoaPods decided to spread some Christmas cheer by making 0.36 Beta available for everybody. You now just need to dosudo gem install cocoapods --pre to install the Beta and not bother with the Gemfile and bundle install steps given below. They are outdated and won’t work anylonger. However you may still need to use custom branch/fork in your Podfile as given in 3rd step and then run pod install to 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, called Gemfile in your project directory and just paste these lines in the file and save:
 
source 'https://rubygems.org'gem 'cocoapods', :git => 'https://github.com/CocoaPods/CocoaPods.git', :branch => 'swift'gem 'cocoapods-core', :git => 'https://github.com/CocoaPods/Core.git'gem 'xcodeproj',  :git => 'https://github.com/CocoaPods/Xcodeproj.git'gem 'claide', :git => 'https://github.com/CocoaPods/CLAide.git'

  • Run command bundle install
  • Create a Podfile. If you want to use Alamofire and SwiftyJSON in your project, your Podfile1 should look like this:
 
source 'https://github.com/CocoaPods/Specs.git'platform :ios, '8.0'pod 'SwiftyJSON', :git => "https://github.com/orta/SwiftyJSON", :branch => "podspec"pod 'Alamofire', :git => "https://github.com/mrackwitz/Alamofire.git", :branch => "podspec"

  • Run bundle exec pod install2 and 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 use3 them.

Hope you liked this post. Connect via Twitter & email for future updates. Thanks for reading.


  1. Each lib should have a podspec file 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. Thanks Brian ofQuick for the tip.↩

  3. Thanks Ambas for the tip about CLAide.↩

 Nov 21st2014 6:38 pm alamofire, cocoapods, code, swiftyjson


0 0
原创粉丝点击