IOS依赖管理工具CocoaPods

作者: ZhongXi | 来源:发表于2015-04-15 00:21 被阅读2390次
    1.CocoaPods简介

    随着IOS开发者的增多,业界也出现了为IOS程序提供依赖管理的工具,它的名字叫作CocoaPods(http://cocoapods.org/).
    GitHub地址:https://github.com/CocoaPods/CocoaPods

    duang
    2.CocoaPods的安装

    CocoaPods的安装方式比较简单,Mac下自带ruby,使用ruby的gem命令即可下载安装:

    $ sudo gem install cocoapods
    $ pod setup
    

    另外,ruby的软件源rubygems.org因为使用亚马逊的云服务,所以被屏蔽了,需要更新一下ruby的源,下面的代码将官方的ruby源替换成国内淘宝的源:

    gem sources --remove https://rubygems.org/
    gem sources -a https://ruby.taobao.org/
    gem sources -l
    

    还有一点需要注意,pod setup在执行时,会输出Setting up CocoaPods master repo,但是会等待比较久的时间,这步其实是CocoaPods在将它的信息下载到~/.cocoapods目录下,如果你等太久,可以试着cd到那个目录,用du -sh *来查看下载进度.

    3.使用CocoaPods的镜像索引

    所有项目的Podspec文件都托管在https://github.com/CocoaPods/Specs. 第一次执行pod setup时,CocoaPods会将这些Podspec索引文件更新到本地的~/.cocoapods/目录下,这个索引文件比较大,有80MB左右,所以第一次更新时非常慢.
    一个叫akinliu(http://akinliu.github.io/2014/05/03/cocoapods-specs-/) 的朋友在gitcafe(http://gitcafe.com/) 和occhina(http://www.oschina.net/) 上建立了CocoaPods索引库的镜像,因为gitcafe和occhina都是国内服务器,所以在执行索引更新操作时,会快很多.如下操作可以将CocoaPods设置成使用gitcafe镜像:

    pod repo remove master
    pod repo add master https://gitcafe.com/akuandev/Specs.git
    pod repo update
    

    将以上代码中的https://gitcafe.com/akuandev/Specs.git替换成http://git.oschina.net/akuandev/Specs.git即可使用occhina上的镜像.

    4.使用CocoaPods

    使用时需要新建一个名为Podfile的文件,以如下格式,将依赖的库名字依次列在文件中即可:(示例:JSONKit和AFNetworking)

    platform :ios
    pod 'JSONKit','~> 1.5pre'
    pod 'AFNetworking','~> 2.5.0'
    

    然后将编辑好的Podfile文件放到项目根目录中,执行如下命令即可:

    cd "你的项目根目录"
    pod install
    

    现在,所有第三方库都已经下载完成并且设置好了编译参数和依赖,你只需要记住如下两点即可:

    • 使用CocoaPods生成的*.xcworkspace文件来打开工程,而不是以前的*.xcodeproj文件
    • 每次更改了Podfile文件,都需要重新执行一次pod update命令
    5.查找第三方库

    如果不知道CocoaPods管理的库中是否有你想要的库,那么你可以通过pod search命令进行查找,以下是我用pod search AFNetworking查找到的所有可用的库:

    -> AFNetworking (2.5.0)
       A delightful iOS and OS X networking framework.
       pod 'AFNetworking', '~> 2.5.0'
       - Homepage: https://github.com/AFNetworking/AFNetworking
       - Source:   https://github.com/AFNetworking/AFNetworking.git
       - Versions: 2.5.0, 2.4.1, 2.4.0, 2.3.1, 2.3.0, 2.2.4, 2.2.3, 2.2.2, 2.2.1,
       2.2.0, 2.1.0, 2.0.3, 2.0.2, 2.0.1, 2.0.0, 2.0.0-RC3, 2.0.0-RC2, 2.0.0-RC1,
       1.3.4, 1.3.3, 1.3.2, 1.3.1, 1.3.0, 1.2.1, 1.2.0, 1.1.0, 1.0.1, 1.0, 1.0RC3,
       1.0RC2, 1.0RC1, 0.10.1, 0.10.0, 0.9.2, 0.9.1, 0.9.0, 0.7.0, 0.5.1 [master
       repo]
       - Sub specs:
         - AFNetworking/Serialization (2.5.0)
         - AFNetworking/Security (2.5.0)
         - AFNetworking/Reachability (2.5.0)
         - AFNetworking/NSURLConnection (2.5.0)
         - AFNetworking/NSURLSession (2.5.0)
         - AFNetworking/UIKit (2.5.0)
    
    -> AFNetworking+AutoRetry (0.0.5)
       Auto Retries for AFNetworking requests
       pod 'AFNetworking+AutoRetry', '~> 0.0.5'
       - Homepage: https://github.com/shaioz/AFNetworking-AutoRetry
       - Source:   https://github.com/shaioz/AFNetworking-AutoRetry.git
       - Versions: 0.0.5, 0.0.4, 0.0.3, 0.0.2, 0.0.1 [master repo]
    
    -> AFNetworking+Ext (1.1)
       AFNetworking的封装, 并提供一个 UIImageView+DYLoading  cache in fileSystem+memory
       pod 'AFNetworking+Ext', '~> 1.1'
       - Homepage: https://github.com/junhaiyang/AFNetworkingExt
       - Source:   https://github.com/junhaiyang/AFNetworkingExt.git
       - Versions: 1.1, 1.0, 0.5, 0.4, 0.3 [master repo]
       - Sub specs:
         - AFNetworking+Ext/Base (1.1)
         - AFNetworking+Ext/AFCustomRequestOperation (1.1)
         - AFNetworking+Ext/AFDownloadRequestOperation (1.1)
         - AFNetworking+Ext/AFTextResponseSerializer (1.1)
         - AFNetworking+Ext/example (1.1)
         - AFNetworking+Ext/UIKit (1.1)
         - AFNetworking+Ext/UIKit/UIImageView+DYLoading (1.1)
    
    -> AFNetworking+ImageActivityIndicator (1.0.1)
       AFNetworking+ImageActivityIndicator makes it easy to show an activity
       indicator while an image view's image is loading using AFNetworking.
       pod 'AFNetworking+ImageActivityIndicator', '~> 1.0.1'
       - Homepage:
       https://github.com/JRG-Developer/AFNetworking-ImageActivityIndicator
       - Source:  
       https://github.com/JRG-Developer/AFNetworking-ImageActivityIndicator.git
       - Versions: 1.0.1, 1.0.0 [master repo]
    
    -> AFNetworking+SingleBlock (0.0.1)
       Categories that aggregate the success and failure block in one single
       solution
       pod 'AFNetworking+SingleBlock', '~> 0.0.1'
       - Homepage: https://github.com/acerbetti/AFNetworking-SingleBlock
       - Source:   https://github.com/acerbetti/AFNetworking-SingleBlock.git
       - Versions: 0.0.1 [master repo]
    
    -> AFNetworking+streaming (0.6)
       A very quick extension to AFNetworking that adds stream based parsing
       pod 'AFNetworking+streaming', '~> 0.6'
       - Homepage: https://github.com/deanWombourne/AFNetworking-streaming
       - Source:   https://github.com/deanWombourne/AFNetworking-streaming.git
       - Versions: 0.6, 0.5, 0.4, 0.3, 0.2, 0.1 [master repo]
       - Sub specs:
         - AFNetworking+streaming/Core (0.6)
         - AFNetworking+streaming/Json (0.6)
    // ...此处省略若干行
    

    @唐巧 <IOS开发进阶>



    End

    相关文章

      网友评论

        本文标题:IOS依赖管理工具CocoaPods

        本文链接:https://www.haomeiwen.com/subject/czgjfttx.html