CocoaPods 安装和使用

1,936 阅读3分钟

CocoaPods是一个用于快速为项目引入第三方库的工具, 可以避免逐个添加多个第三方库到项目的麻烦, 集中管理, 提高效率,本文记录了在Mac OS Monterey12.1上的安装步骤和使用方法, 不同系统可能存在差异。

一、 安装

1.1 Ruby

cocoapods通过ruby实现, 准备工作需要确认是否成功安装

1.1.1 查看替换源

// 更新
sudo gem update -n /usr/local/bin —system

// 查看源
gem sources -l

// 输出
*** CURRENT SOURCES ***
https://gems.ruby-china.com/

如果你的源还是rubygems.org 为了避免cocopods的安装过于缓慢 需要进行替换, 移除原有的源, 添加gems.ruby-china.com/ 作为新的源

gem sources --remove https://rubygems.org/
gem sources -a https://gems.ruby-china.com/

1.2 cocoapods安装

1.2.1 安装cocoapods工具

sudo gem install -n /usr/local/bin cocoapods

//输出
Successfully installed cocoapods-1.11.3
Parsing documentation for cocoapods-1.11.3
Done installing documentation for cocoapods after 0 seconds
1 gem installed
  • 注意
    • 这里可能会出现没有对应目录权限不让安装的情况
    • 尝试修改对应目录权限进行install, 但操作不被允许
    • 尝试使用brew再安装一个ruby, 代替系统的ruby,brew install ruby, 我不确定是否是这个起作用了, 当我重新执行sudo gem install -n /usr/local/bin cocoapods, 没有再出现之前的目录权限问题

1.2.2 下载cocopods的仓库

pod setup

// 输出
Setup completed

1.2.3 查看是否下载成功

pod search AFNetworking

//输出
[!] Unable to find a pod with name, author, summary, or description matching `AFNetworking`

pod repo list
//输出
0 repo

这里我们没有办法找到AFNetworking的第三方库, 本身cocoapods的仓库也没有, 说明仓库下载出了问题

1.2.4 重新下载cocoapods的仓库

// 这里我们本来就没有repo 可跳过
pod repo remove master

// 进入 ~/.cocoapods/repos, 这个目录不存在则创建
mkdir ~/.cocoapods/repos

// 重新下载repo 这里需要等待一段时间, 输出会显示进度
git clone --depth 1 https://github.com/CocoaPods/Specs.git master

// 移除之前存储的索引文件
rm ~/Library/Caches/CocoaPods/search_index.json

// 再次进行查找, 触发重新下载
pod search AFNetworking

// 输出 并且会开启新的窗口显示查找结果
Creating search index for spec repo 'master'.. Done!
[!] Skipping `OCast` because the podspec contains errors.
[!] Skipping `OCastReferenceDriver` because the podspec contains errors.
[!] Skipping `TEST-MiniPlengi` because the podspec contains errors.

二、 使用

2.1 查找第三方库

pod search AFNetworking

// 输出 可能需要等待一段时间 搜索结果展示, 其中也包含了我们导入时需要的命令格式

-> MJRefresh (3.7.5)
   An easy way to use pull-to-refresh
   pod 'MJRefresh', '~> 3.7.5'
   - Homepage: https://github.com/CoderMJLee/MJRefresh
   - Source:   https://github.com/CoderMJLee/MJRefresh.git
   - Versions: 3.7.5, 3.7.4, 3.7.3, 3.7.2, 3.7.1, 3.6.1, 3.6.0, 3.5.0, 3.4.3, 3.4.1, 3.4.0, 3.3.1, 3.2.3, 3.2.2, 3.2.0, 3.1.17, 3.1.16, 3.1.15.7, 3.1.15.6,
   3.1.15.3, 3.1.15.2, 3.1.15.1, 3.1.15, 3.1.14.1, 3.1.14, 3.1.13, 3.1.12, 3.1.11, 3.1.10, 3.1.9, 3.1.8, 3.1.7, 3.1.6, 3.1.5, 3.1.4, 3.1.3, 3.1.2, 3.1.1,
   3.1.0, 3.0.8, 3.0.7, 3.0.6, 3.0.5, 3.0.4, 3.0.3, 3.0.2, 3.0.1, 3.0.0, 2.5.0, 2.4.12, 2.4.11, 2.4.10, 2.4.9, 2.4.8, 2.4.7, 2.4.6, 2.4.5, 2.4.4, 2.4.3,
   2.4.2, 2.4.1, 2.4.0, 2.3.2, 2.3.1, 2.3.0, 2.2.1, 2.2.0, 2.1.0, 2.0.4, 2.0.3, 2.0.2, 2.0.1, 2.0.0, 1.4.7, 1.4.6, 1.4.5, 1.4.4, 1.4.3, 1.4.1, 1.4.0, 1.3.8,
   1.3.7, 1.3.6, 1.3.5, 1.3.4, 1.3.3, 1.3.2, 1.3.1, 1.3.0, 1.2.0, 1.1.0, 1.0.5, 1.0.4, 1.0.3, 0.0.1 [master repo]

-> MJRefresh-fork (3.2.1)
   An easy way to use pull-to-refresh
   pod 'MJRefresh-fork', '~> 3.2.1'
   - Homepage: https://github.com/CoderMJLee/MJRefresh
   - Source:   https://github.com/sugerGDev/MJRefresh-fork.git
   - Versions: 3.2.1 [master repo]
   
...

2.2 项目引入第三方库

2.2.1 添加Podfile

进入项目.xcodeproj文件所在目录, 新建Podfile, 文件内指明需要安装的第三方库及版本

// 这个命令帮助生成一个Podfile文件
pod init
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'CocoapodsTest' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!
  # Pods for CocoapodsTest
  
pod 'AFNetworking', '~> 4.0.1'
pod 'Masonry', '~> 1.1.0'
pod 'MJExtension', '~> 3.4.1'
pod 'MJRefresh', '~> 3.7.5'

end

2.2.2 安装第三方库

pod install

//输出 
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (4.0.1)
Installing MJExtension (3.4.1)
Installing MJRefresh (3.7.5)
Installing Masonry (1.1.0)
Generating Pods project
Integrating client project

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

途中也可能因为网络问题出错而中断, 重新pod install即可

// 输出
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (4.0.1)
Installing MJExtension (3.4.1)

[!] Error installing MJExtension
[!] /usr/bin/git clone https://github.com/CoderMJLee/MJExtension.git /var/folders/y3/c38ylwq54gx098qr16kss5700000gn/T/d20220324-80601-hd2owl --template= --single-branch --depth 1 --branch 3.4.1

Cloning into '/var/folders/y3/c38ylwq54gx098qr16kss5700000gn/T/d20220324-80601-hd2owl'...
fatal: unable to access 'https://github.com/CoderMJLee/MJExtension.git/': LibreSSL SSL_connect: Connection reset by peer in connection to github.com:443

2.2.3 使用第三方库

用.xcworkspace打开项目, 之后用#import <> 引入对应头文件即可 image.png

参考文章