CocoaPods 使用

Ruby是一种面向对象编程语言。
RubyGems 是一个 Ruby 程序包管理器,它提供一个分发 Ruby 程序和库的标准格式,还提供一个管理程序包安装的工具。通过 RubyGems 来管理 CocoaPods 。
CocoaPods 是一个 Cocoa 和 Cocoa Touch 框架的依赖管理器,原理是从 GitHub 下载索引,然后根据索引下载依赖的源代码。

RubyGems 相关操作

查看 RubyGems 源

$ gem sources -l

移除 RubyGems 源

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

添加 RubyGems 源

$ gem sources -a https://gems.ruby-china.com/

查看 RubyGems 版本

$ gem --version

更新 RubyGems 版本

$ sudo gem update --system

CocoaPods 相关操作

通过 RubyGems 安装和更新 CocoaPods

$ sudo gem install cocoapods

通过 RubyGems 卸载 CocoaPods

$ sudo gem uninstall cocoapods

查看 CocoaPods 版本

$ pod --version
pod install 操作

在每次编辑 Podfile 文件以添加、移除 pods 时使用。运行该命令只会增加和移除 pods ,不会更新 pods。

$ pod install
pod update 操作

当需要更新某个 pod 时,使用该命令使其更新到最新版本。

$ pod update PODNAME

当运行$ pod update会更新所有的 pods。

CocoaPods 使用注意事项

使用 CocoaPods 时,如果无法访问外网,会导致操作失败,此时有两种思路解决该问题。

思路一:使用代理访问外网(推荐)

使用代理访问外网,不需要做其他额外操作,并且会使用 CocoaPods 默认源操作,安全稳定。
注意设置代理为全局代理。

思路二:使用国内 CocoaPods 镜像源(不推荐)

添加国内 CocoaPods 镜像源到本地,并在 Podfile 文件中指定 source。

方式1

$ pod repo remove master
$ pod repo add master https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git
$ pod repo update

方式2

$ cd ~/.cocoapods/repos 
$ pod repo remove master
$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git master

注意:名为 trunk 的 repo 为系统默认的源,所以我们添加时不能命名为 trunk。
Podfile 文件中添加

source 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git'

指定 CocoaPods 源。

Podfile 使用和说明

1、终端进入目标工程根目录:cd PROJECTPATH
2、运行命令 pod init (推荐) 或者 touch Podfile
3、编辑 Podfile 文件
4、pod install

Podfile 文件模板:
platform: ios, '9.0'
inhibit_all_warnings!

target 'AgentHouse' do
    pod 'AFNetworking'
    pod 'Masonry'
    pod 'SDWebImage', '~> 5.0'
end
说明:

platform: ios, '9.0' 指定系统和支持的最低版本
inhibit_all_warnings! 忽略所有第三方库带来的警告

版本控制说明

'> 0.1' 高于0.1的任何版本
'>= 0.1' 版本0.1或更高版本
'< 0.1' 低于0.1的任何版本
'<= 0.1' 版本0.1或更低的版本

'~> 0.1.2' 版本0.1.2和0.2版本之间的任意版本,包括版本0.1.2,不包括0.2版本
'~> 0.1' 版本0.1和版本1.0之间的任意版本,包括版本0.1,不包括1.0版本

CocoaPods 官方文档
清华大学开源软件镜像站

posted @ 2022-01-07 17:32  ebamboo  阅读(408)  评论(0编辑  收藏  举报