分享

CocoaPods 安装使用

 wintelsui 2014-01-25
     CocoaPods是一个负责管理iOS项目中第三方开源代码的工具。CocoaPods项目的源码在Github上管理。该项目开始于2011年8月12日,经过一年多的发展,现在已经超过1000次提交,并且持续保持活跃更新。开发iOS项目不可避免地要使用第三方开源库,CocoaPods的出现使得我们可以节省设置和更新第三方开源库的时间。

安装

在安装之前,运行以下命令

Ruby代码  收藏代码
  1. gem update --system  


可以升级系统当前的ruby版本

安装方式异常简单, Mac下都自带ruby,通过终端(Terminal)使用ruby的gem命令即可下载安装:

Ruby代码  收藏代码
  1. $ gem install cocoapods  
  2. $ pod setup  


Ruby代码  收藏代码
  1. gem install cocoapods  

命令也可以自动升级当前cocoapods版本
一旦安装完毕,通过运行
Ruby代码  收藏代码
  1. pod setup  

命令来设置CocoaPods

Once installed, setup CocoaPods by running the pod setup command. During the setup process, the CocoaPods environment is formed and a .cocoapods directory is created in your home folder. This hidden folder contains all the available pod specifications or pod specs.

A dependency declaration has a lot more configuration options, which can be set in the Podfile. If you want to work with the bleeding edge version of a library, for example, you can replace a pod’s version number with :head as shown below. You can even tell CocoaPods what source to use by specifying the git repository or referring CocoaPods to a local copy of the library. These are more advanced features of CocoaPods.
Ruby代码  收藏代码
  1. pod 'AFNetworking', :head  
  2. pod 'SVProgressHUD', :git => 'https://github.com/samvermette/SVProgressHUD'  
  3. pod 'ViewDeck', :local => '~/Development/Library/ViewDeck'  
  4.   
  5. pod 'DTCoreText', :git => 'https://github.com/Cocoanetics/DTCoreText.git'  


With our list of dependencies specified, it is time to continue the setup process. Update the Podfile as shown below and run pod install in the Terminal. Make sure to run this command in the root of your Xcode project where you also created the project’s Podfile.

Ruby代码  收藏代码
  1. platform :ios, '6.0'  
  2. pod 'ViewDeck', '~> 2.2.2'  
  3. pod 'AFNetworking', '~> 1.1.0'  
  4. pod 'SVProgressHUD', '~> 0.9.0'  
  5. pod 'HockeySDK', '~> 3.0.0'  
  6.   
  7. pod install  



使用:
Open Terminal, and navigate to the folder where your project is. Type the following:
Terminal代码  收藏代码
  1. touch Podfile  
  2. open -e Podfile  


TextEdit should open up showing an empty file. You just created the pod file and opened it! Ready to add some content to the empty pod file?
Copy and paste the following lines into the TextEdit window:
Terminal代码  收藏代码
  1. platform :ios  
  2.   
  3. pod 'AFNetworking', '0.9.1'  

Now, save the file and close TextEdit, going back to terminal. Ready to configure your project?
Issue the following command via Terminal:
Terminal代码  收藏代码
  1. pod install  



You should see output similar to the following:
Terminal代码  收藏代码
  1. Updating spec repo `master'  
  2. Installing AFNetworking (0.9.1)  
  3. Generating support files  


It might also tell you something like this:
Terminal代码  收藏代码
  1. [!] From now on use `CocoaPodsExample.xcworkspace'  


If you type ls now (or browse the project folder using Finder), you’ll see that CocoaPods created a Pods folder – where it stores all dependencies – and CocoaPodsExample.xcworkspace.

Terminal代码  收藏代码
  1. VERY IMPORTANT!  
  2. From now on, as the command-line warning mentioned, you must always open the workspace (CocoaPodsExample.xcworkspace) and not the project!  


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

使用CocoaPods生成的 .xcworkspace 文件来打开工程,而不是以前的 .xcodeproj 文件。
每次更改了Podfile文件,你需要重新执行一次pod install命令

安装DTCoreText一定要注意Other Linker Flags的属性值
-all_load和-ObjC要满足


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多