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

CocoaPods对Swift程序的支持的更多相关文章

  1. Xcode 8和iOS 10支持SwiftyJSON吗?

    我最近将我的SwiftiOS应用程序从Xcode7升级到Xcode8,并将部署平台从iOS9.3升级到iOS10.我的应用程序开始出现这样的错误:Modulefilewascreatedbyanolderversionofthecompiler:rebuild‘SwiftyJSON’andtryagain.我用Google搜索并在SOModulefilewascreatedbyanolderver

  2. PhoneGap / iOS上的SQLite数据库 – 超过5mb可能

    我误解了什么吗?Phonegap中的sqlitedbs真的有5mb的限制吗?我正在使用Phonegap1.2和iOS5.解决方法您可以使用带有phonegap插件的原生sqliteDB,您将没有任何限制.在iOS5.1中,Websql被认为是可以随时删除的临时数据…

  3. ios – 使用带有NodeJs HTTPS的certificates.cer

    我为IOS推送通知生成了一个.cer文件,我希望将它与NodeJSHTTPS模块一起使用.我发现HTTPS模块的唯一例子是使用.pem和.sfx文件,而不是.cer:有解决方案吗解决方法.cer文件可以使用两种不同的格式进行编码:PEM和DER.如果您的文件使用PEM格式编码,您可以像使用任何其他.pem文件一样使用它(有关详细信息,请参见Node.jsdocumentation):如果您的文件使

  4. cocoapods – 命令/ bin / sh失败,退出代码23

    适用于所有豆荚,无需豆荚但仍然是同样的错误.有任何想法吗?

  5. iOS编译库/框架链接器标记问题是设备编译而不是模拟器

    背景:我正在尝试使用带有cocoapods的Phonegap.Phonegap已经存在从命令行编译的问题所以我只是将Xcode与生成的项目文件一起使用(并且在使用cocoapods时我无论如何都必须这样做).我用我的问题向Github推送了一个复制的空项目:https://github.com/Dan2552/phonegap-reproducable-issue您可以在repo的根目录中的bui

  6. ios – CFNetwork内部错误:URLConnectionLoader.cpp:289

    当我在一段时间后打开我的应用程序时,我收到了日志:440:CFNetworkinternalerror(0xc01a:/buildroot/Library/Caches/com.apple.xbs/Sources/CFNetwork/CFNetwork-758.4.3/Loading/URLConnectionLoader.cpp:289)它从未出现在过去.我的项目使用网络库AFNetworkin

  7. ios – 使用大写符号在字符串swift中获取URL的正则表达式

    我尝试在文本中获取URL.所以,在此之前,我使用了这样一个表达式:但是当用户输入带有大写符号的URL时(例如Http://Google.com,它与它不匹配)我遇到了问题.我试过了:但什么都没发生.解决方法您可以使用正则表达式中的i内联标志关闭区分大小写,有关可用正则表达式功能的详细信息,请参阅FoundationFrameworkReference.(?ismwx-ismwx)Flagsetti

  8. ios – CocoaPods CorePlot安装显示缺少的框架和丢失的文件

    我是CocoaPods的新手,我正在尝试在我的Podfile中使用此行安装CorePlot:当我在安装后打开.xcworkspace文件时,CorePlot会显示缺少的框架.我将这些链接的二进制文件安装到我的Xcode项目中,但这并没有解决问题.在CorePlot目标中,似乎没有单独的方法来安装框架.这不是唯一的问题.当我尝试在CorePlot库中引用文件时,即使我可以看到CorePlot目标中的文件,我也会收到文件未找到错误:我尝试了以下import语句:都导致同样的错误.我不知道这些问题是否相关.我应

  9. 通用iOS应用程序在TestFlight中的iPad Air 2上不兼容

    您应该为项目和目标设置“有效架构”:armv7,armv7s和arm64.

  10. ios – 如何在不创建新目标的情况下阻止`pod install`将libPods.a添加到“Link with Libraries”构建阶段?

    解决方法要防止集成,您可以使用:

随机推荐

  1. Swift UITextField,UITextView,UISegmentedControl,UISwitch

    下面我们通过一个demo来简单的实现下这些控件的功能.首先,我们拖将这几个控件拖到storyboard,并关联上相应的属性和动作.如图:关联上属性和动作后,看看实现的代码:

  2. swift UISlider,UIStepper

    我们用两个label来显示slider和stepper的值.再用张图片来显示改变stepper值的效果.首先,这三个控件需要全局变量声明如下然后,我们对所有的控件做个简单的布局:最后,当slider的值改变时,我们用一个label来显示值的变化,同样,用另一个label来显示stepper值的变化,并改变图片的大小:实现效果如下:

  3. preferredFontForTextStyle字体设置之更改

    即:

  4. Swift没有异常处理,遇到功能性错误怎么办?

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  5. 字典实战和UIKit初探

    ios中数组和字典的应用Applicationschedule类别子项类别名称优先级数据包contactsentertainment接触UIKit学习用Swift调用CocoaTouchimportUIKitletcolors=[]varbackView=UIView(frame:CGRectMake(0.0,0.0,320.0,CGFloat(colors.count*50)))backView

  6. swift语言IOS8开发战记21 Core Data2

    上一话中我们简单地介绍了一些coredata的基本知识,这一话我们通过编程来实现coredata的使用。还记得我们在coredata中定义的那个Model么,上面这段代码会加载这个Model。定义完方法之后,我们对coredata的准备都已经完成了。最后强调一点,coredata并不是数据库,它只是一个框架,协助我们进行数据库操作,它并不关心我们把数据存到哪里。

  7. swift语言IOS8开发战记22 Core Data3

    上一话我们定义了与coredata有关的变量和方法,做足了准备工作,这一话我们来试试能不能成功。首先打开上一话中生成的Info类,在其中引用头文件的地方添加一个@objc,不然后面会报错,我也不知道为什么。

  8. swift实战小程序1天气预报

    在有一定swift基础的情况下,让我们来做一些小程序练练手,今天来试试做一个简单地天气预报。然后在btnpressed方法中依旧增加loadWeather方法.在loadWeather方法中加上信息的显示语句:运行一下看看效果,如图:虽然显示出来了,但是我们的text是可编辑状态的,在storyboard中勾选Editable,再次运行:大功告成,而且现在每次单击按钮,就会重新请求天气情况,大家也来试试吧。

  9. 【iOS学习01】swift ? and !  的学习

    如果不初始化就会报错。

  10. swift语言IOS8开发战记23 Core Data4

    接着我们需要把我们的Rest类变成一个被coredata管理的类,点开Rest类,作如下修改:关键字@NSManaged的作用是与实体中对应的属性通信,BinaryData对应的类型是NSData,CoreData没有布尔属性,只能用0和1来区分。进行如下操作,输入类名:建立好之后因为我们之前写的代码有些地方并不适用于coredata,所以编译器会报错,现在来一一解决。

返回
顶部