iOS开发~CocoaPods使用详细说明

来源:互联网 发布:云计算入门书籍 编辑:程序博客网 时间:2024/06/06 12:31

文章来源:http://blog.csdn.net/lizhongfu2013/article/details/26384029

推荐文章:http://code4app.com/article/cocoapods-install-usageCocoaPods安装和使用教程


一、概要

iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库。


二、安装

由于网上的教程基本都大同小异,但细节之处还不是很完善,所以借机会在这里补充下:

注:要使用CocoaPods,那就要下载安装它,而下载安装CocoaPods需要Ruby环境


1、Ruby环境搭建

当前安装环境为Mac mini 10.8.5。Mac  OS本身自带Ruby,但还是更新一下保险,因为我第一次安装在没有更新Ruby的情况下就失败了。

a 查看下当前ruby版本:打开终端输入 ruby -v(确实安装了,不过用这个版本接下来工作失败了,所以更新下ruby)

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. ritekiMac-mini:PodTest lucky$ ruby -v  
  2. ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]  
  3. ritekiMac-mini:PodTest lucky$   

b 更新ruby

终端输入如下命令(把Ruby镜像指向taobao,避免被墙,你懂得

gem sources --remove https://rubygems.org/ 
gem sources -a http://ruby.taobao.org/ 
gem sources -l  (用来检查使用替换镜像位置成功)

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. ritekiMac-mini:~ lucky$ gem sources --remove https://rubygems.org/  
  2. https://rubygems.org/ removed from sources  
  3. ritekiMac-mini:~ lucky$ gem sources -a http://ruby.taobao.org/  
  4. http://ruby.taobao.org/ added to sources  
  5. ritekiMac-mini:~ lucky$ gem sources -l  
  6. *** CURRENT SOURCES ***  


2、下载安装CocoaPods

终端输入:sudo gem install cocoapods 


[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. ritekiMac-mini:~ lucky$ sudo gem install cocoapods  
  2.   
  3. CHANGELOG:  
  4.   
  5. ## 0.32.1  
  6.   
  7. ##### Bug Fixes  
  8.   
  9. * Fixed the Podfile `default_subspec` attribute in nested subspecs.    
  10.   [Fabio Pelosin][irrationalfab]  
  11.  \ [#2050](https://github.com/CocoaPods/CocoaPods/issues/2050)  
  12.   
  13.   
  14. Successfully installed cocoapods-0.32.1  
  15. Installing ri documentation for cocoapods-0.32.1  
  16. /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rdoc/rdoc.rb:280: warning: conflicting chdir during another chdir block  
  17. /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rdoc/rdoc.rb:287: warning: conflicting chdir during another chdir block  
  18. Done installing documentation for cocoapods after 10 seconds  
  19. 1 gem installed  


这样就下载安装好了CocoaPods


3、使用CocoaPods

a  新建一个项目,名字PodTest



b  终端中,cd到项目总目录(注意:包含PodTest文件夹、PodTest.xcodeproj、PodTestTest的那个总目录)

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. cd /Users/lucky/Desktop/PodTest   

c  建立Podfile(配置文件)

接着上一步,终端输入 vim Podfile



键盘输入 i,进入编辑模式,输入

platform :ios, '7.0' 
pod 'MBProgressHUD', '~> 0.8'


然后按Esc,并且输入“ :”号进入vim命令模式,然后在冒号后边输入wq



注意:键盘输入 :后,才能输入wq。回车后发现PodTest项目总目录中多一个Podfile文件



激动人心的时刻到了:确定终端cd到项目总目录,然后输入 pod install,等待一会,大约3分钟。



查看项目根目录:



注意:现在打开项目不是点击 PodTest.xodeproj了,而是点击 PodTest.xcworkspace


打开项目后看到项目结构并且测试一下:



运行结果:



补充:

1、CocoaPods的基本安装及使用都详细的说明了,但还有一些补充,当需要同时导入多个第三方时候怎么办 ?

这就需要修改Podfile了,就是用vim编辑的那个保存在项目根目录中的文件,修改完了Podfile文件,需要重新执行一次pod install命令。

例如:

platform :ios

pod 'JSONKit',       '~> 1.4'

pod 'AFNetworking',  '~> 2.0'


2、CocoaPods可以查找你想要的第三方库

终端输入命令:pod search UI

疯了了,我怎么查找这么大众的关键字,好多库~~


然后重新编辑Podfile文件,按照之前的步骤,把更多的库都导入项目!


Links

LinkDescriptionCocoaPods.orgHomepage and search for Pods.@CocoaPodsFollow CocoaPods on Twitter to stay up to date.BlogThe CocoaPods blog.Mailing ListFeel free to ask any kind of question.GuidesEverything you want to know about CocoaPods.ChangelogSee the changes introduced in each CocoaPods version.New Pods RSSDon't miss any new Pods.Code of ConductFind out the standards we hold ourselves to.

0 0
原创粉丝点击