CocoaPods安装和使用教程以及Updating local specs repositories问题

来源:互联网 发布:linux如何进行源码安装 编辑:程序博客网 时间:2024/04/30 00:59

一、概要

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 https://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 https://ruby.taobao.org/  
  4. https://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分钟。




如果卡在下图所示问题,不必担心,这是更新本地的pod仓库,和git一样,本地有个pod repo,和github上的版本对应,多等一会就可以了。如果你不想更新这个的话,可以将pod install  改成pod install --verbose --no-repo-update就可以了,但是这样会有个问题,如果github上pods的一些插件像AF有新版本了,你本地搜索的af还是旧版本如果用的新版本号是无法装配的,所以每隔一段时间我都会执行一下pod repo update,--verbose的意思是打印详细信息



查看项目根目录:



注意:现在打开项目不是点击 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文件,按照之前的步骤,把更多的库都导入项目!



可能出现的问题:

[!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.

出现这个问题的原因很简单,别害怕。这是因为你写的‘ ’ 这个符号有问题   你用Xcode打开podfile,如果看到8.0两边的引号是这样的,你需哟用xcode重写一下,让它变成下面紫色那种引号,然后上面的问题就解决了。



问题二:

CocoaPods报错:The dependency `AFNetworking ` is not used in any

concrete target

你要用下面这种格式 其中MyApp 就是你工程的target 一般就是工程名

platform :ios, '8.0'#use_frameworks!target 'MyApp' do  pod 'AFNetworking', '~> 2.6'  pod 'ORStackView', '~> 3.0'  pod 'SwiftyJSON', '~> 2.3'end




0 0