Cocoapods的安装与使用

6,651 阅读1分钟

CocoaPods 官网

安装

CocoaPods安装需要依赖 RubyMac已自带Ruby环境

$ gem -v
3.0.3

Ruby的软件源使用的是国外的,如果没有科学上网国内使用可能会出现不稳定、速度慢之类的问题

$ gem sources
*** CURRENT SOURCES ***

https://rubygems.org/

建议移除国外源替换为国内的China ruby 源

//移除
 $ gem sources --r https://rubygems.org/
https://rubygems.org/ removed from sources
//添加
$ gem sources --a https://gems.ruby-china.com
https://gems.ruby-china.com added to sources

Ruby用管理员权限安装Cocoapods

 $ sudo gem install cocoapods

过程简述

Password:
//输入密码开始安装
Fetching concurrent-ruby-1.1.5.gem
......
//安装完成
Done installing documentation for thread_safe
......
cocoapods after 25 seconds
29 gems installed

通过gem查看cocoapods 相关安装列表

$ gem list --local | grep cocoapods
cocoapods (1.8.4)
cocoapods-core (1.8.4)
cocoapods-deintegrate (1.0.4)
cocoapods-downloader (1.2.2)
cocoapods-plugins (1.0.0)
cocoapods-search (1.0.0)
cocoapods-stats (1.1.0)
cocoapods-trunk (1.4.1)
cocoapods-try (1.1.0)

更新

//pod更新最新版本
sudo gem install -n /usr/local/bin cocoapods --pre

//pod 更新到指定版本
sudo gem install -n /usr/local/bin cocoapods -v 1.4.0

卸载

//上面cocoapods列表依次执行
$ sudo gem uninstall cocoapods
$ sudo gem uninstall cocoapods-core
......

使用

这条命令是将Github上的pod 托管的Podspec索引都安装到本地

$ pod setup
Setting up CocoaPods master repo
...

正常情况下应该是经过漫长的等待后 pod仓库拉取配置完成

然而不知道什么原因我的电脑 $pod setup 什么都没有发生就直接结束了,什么也不报错

那这个时候怎么办,一想不就是给 pod添加索引库嘛?

  • plan 1

那我就手动添加试一下咯

$ pod repo add master https://github.com/CocoaPods/Specs.git
Cloning spec repo `master` from `https://github.com/CocoaPods/Specs.git`
......

这个过程也是非常漫长的,等了好一会,我新开了一个终端查看了下当前的已下载的文件大小

$ cd ~/.cocoapods
$ du -sh *
43M repos

等待总是很烦的,按照这个下载速度我要等到花都谢了,本来以为等就完了,然而出现了下面的错误

[!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git -- master

Cloning into 'master'...
error: RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
fatal: the remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed

出现这问题其实就是你当前网络环境不好,多试几次或则换个网络再有就是科学上网也试试,等到终端结束到下一个命令行的开始就添加成功了

  • plan 2

找个已经安装好 pod索引库的电脑,把~/.cocoapods/repos 路径下的master 直接压缩拷贝,然后解压到你电脑相同目录即可

  • plan 3

直接去https://github.com/CocoaPods/Specs.git下载 zip,同样解压到plan 2的目录,并把文件夹名字修改为 master


通过pod查看已添加的 repo

$ pod repo list

master
- Type: git (master)
- URL:  https://github.com/CocoaPods/Specs.git
- Path: /Users/QingGe/.cocoapods/repos/master


  • 常用pod 命令及解释
//生成Podfile文件,编写需要的第三方文件
$ pod init 

//安装第三方库,安装成功会生成`podfile.lock`文件用来记录第三方库版本,后面接上--no--repo--install 的意思是不更新本地索引库
$ pod install 
$ pod install --no--repo--install

//更新第三方库为本地索引库最新版本,后面接上--no--repo--update的意思是不更新本地索引库
$ pod update 
$ pod update --no--repo--update

//搜索当前本地索引仓库中对应的第三方库
$ pod search 第三方库名称

//查看当前版本
$pod --version

//更新本地所有索引库 ,更新指定索引库接上索引库名称即可
$ pod repo update

Demo

1、cd.xcodeproj所在目录

$ cd Desktop/podDemo

2、.xcodeproj同级目录初始化Podfile文件

$ pod init

3、生成 pod 工程,之后都通过.xcworkspace打开工程

$ pod install
Analyzing dependencies
Downloading dependencies
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `podDemo.xcworkspace` for this project from now on.
Pod installation complete! There are 0 dependencies from the Podfile and 0 total pods installed.

[!] The Podfile does not contain any dependencies.

[!] Automatically assigning platform `iOS` with version `13.2` on target `podDemo` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

4、编写并安装所需第三方库 4.1、查找所需第三方库 注意第三库名称不能写错,因为不支持模糊查找

$ pod search AFNetWorking
-> AFNetworking (3.2.1)
   A delightful iOS and OS X networking framework.
   pod 'AFNetworking', '~> 3.2.1'
   - Homepage: https://github.com/AFNetworking/AFNetworking
   - Source:   https://github.com/AFNetworking/AFNetworking.git
   - Versions: 3.2.1, 3.2.0, 3.1.0, 3.0.4, 3.0.3, 3.0.2, 3.0.1, 3.0.0,
   3.0.0-beta.3, 3.0.0-beta.2, 3.0.0-beta.1, 2.7.0, 2.6.3, 2.6.2, 2.6.1, 2.6.0,
   2.5.4, 2.5.3, 2.5.2, 2.5.1, 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]
   - Subspecs:
     - AFNetworking/Serialization (3.2.1)
     - AFNetworking/Security (3.2.1)
     - AFNetworking/Reachability (3.2.1)
     - AFNetworking/NSURLSession (3.2.1)
     - AFNetworking/UIKit (3.2.1)
......
:

复制 pod 'AFNetworking', '~> 3.2.1' ,按 Q 退出

4.2、编写Podfile,vim 准备编辑,i 开始编辑,esc 退出编辑,shift+:输入wq即 保存并退出

$ vim Podfile
# Uncomment the next line to define a global platform for your project
//指定系统版本
# platform :ios, '9.0'

//指定索引源
source "https://github.com/CocoaPods/Specs.git"

target 'podDemo' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for podDemo
  //复制到这里
  pod 'AFNetworking', '~> 3.2.1'
end
......
"Podfile" 10L, 230C
  • ps: 也可以通过 打开 工程找到Podfile文件直接编写

4.3 、安装

$ pod install
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (3.2.1)
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

[!] Automatically assigning platform `iOS` with version `13.2` on target `podDemo` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

4.4、引入(通过< >形式)

#import <AFNetworking/AFNetworking.h>

end