CocoaPods安装及使用

用淘宝的RubyGems镜像来代替官方版本 

$ gem sources -l

$ gem sources --remove https://rubygems.org/

$ gem sources -a https://ruby.taobao.org/

$ gem sources -l

成功后执行命令

pod setup

尝试搜索第三方类库

pod search AFNetworking

建立一个Podfile文件

touch Podfile 或指定到文件夹cd /User/ST/Desk…

然后利用vim打开Podfile文件编辑,加入你想要使用的类库,格式如下

platform :ios

pod 'Reachability', '3.1.0'

platform:ios, '6.0'

pod 'JSONKit','1.4'

pod 'AFNetworking', '~> 2.3.1'

 

如果是拷贝的别人的项目,或是一个很久没打开过的项目,可能需要先执行一下:

pod update

最后一步,执行命令:

pod install

 

—————版本号写法—————

pod ‘AFNetworking’      //不显式指定依赖库版本,表示每次都获取最新版本

pod ‘AFNetworking’,  ‘2.0’     //只使用2.0版本

pod ‘AFNetworking’, ‘>2.0′     //使用高于2.0的版本

pod ‘AFNetworking’, ‘>=2.0′     //使用大于或等于2.0的版本

pod ‘AFNetworking’, ‘<2.0′     //使用小于2.0的版本

pod ‘AFNetworking’, ‘<=2.0′     //使用小于或等于2.0的版本

pod ‘AFNetworking’, ‘~>0.1.2′     //使用大于等于0.1.2但小于0.2的版本,相当于>=0.1.2并且<0.2.0

pod ‘AFNetworking’, ‘~>0.1′     //使用大于等于0.1但小于1.0的版本

pod ‘AFNetworking’, ‘~>0′     //高于0的版本,写这个限制和什么都不写是一个效果,都表示使用最新版本

 这个问题时

  1. Could not automatically select an Xcode project. Specify one in your Podfile like so:  
  2.   
  3. xcodeproj 'path/to/Project.xcodeproj'  

在Podfile文件里指定下工程目录就行了,比如我在Podfile文件添加这行就行了:

 

  1. ......  
  2. xcodeproj 'Portfolio/Portfolio.xcodeproj'   
  3. ......  

主要是让Pod找到子目录中的工程文件。

当在update或install时遇到这个问题:

  1. Unable to find a specification for `xxxxx (~> 1.x.x)` depended upon by Podfile.  

只需要把当前Pod的目录清理一下就行了。在终端执行以下命令:

  1. pod repo remove master  
  2. pod setup  

setup成功后执行install或update即可。

原文地址:https://www.cnblogs.com/st-cool/p/5177441.html