Mac安装cocoapods
Mac安装cocoapods

Mac安装cocoapods

前期摘要

在学习Cocoapods时,执行 pod install 命令时,显示:

[!] Error installing Firebase
[!] /usr/bin/git clone https://github.com/firebase/firebase-ios-sdk.git /var/folders/1y/t7s8thm536j0hgvd3fhdp7pr0000gn/T/d20240515-43435-8lq30m --template= --single-branch --depth 1 --branch CocoaPods-10.25.0

Cloning into '/var/folders/1y/t7s8thm536j0hgvd3fhdp7pr0000gn/T/d20240515-43435-8lq30m'...
fatal: unable to access 'https://github.com/firebase/firebase-ios-sdk.git/': Recv failure: Connection reset by peer

上述报错,因此决定将安装cocoapods的过程和发现的问题汇总整理。

环境配置

mac Sonoma 14.4.1

下载cocoapods

使用Cocoapods官网(https://cocoapods.org/)的安装命令:

$ sudo gem install cocoapods

如果安装过程缓慢,可以使用brew进行安装,相关差异在之前的文章中有表述:

$ brew install cocoapod

初始化cocoapods

在Xcode项目目录中初始化一个cocoapods项目

pod init

相关操作如下:

% cd /Users/fangjunyu/Downloads/TestTask 
TestTask % ls
TestTask        TestTaskTests
TestTask.xcodeproj  TestTaskUITests
TestTask % pod init
TestTask % ls
Podfile         TestTask.xcodeproj  TestTaskUITests
TestTask        TestTaskTests

通过上述代码可以看到,使用pod init命令会创建一个Podfile文件

添加镜像源

移除现有的trunk源(如果已经存在):

pod repo remove trunk

添加镜像源,并将其命名为不同于trunk的名称:

pod repo add TsinghuaSpecs 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git'

更新Podfile

编辑Podfile文件的内容

TestTask % vi Podfile

# 指定使用的源
source 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git'
source 'https://cdn.cocoapods.org/'

platform :ios, '14.0'

target 'TestTask' do
  use_frameworks!

  # 添加Firebase Pods
  pod 'Firebase/Analytics'
  pod 'Firebase/Core'
  pod 'Firebase/Firestore'
  pod 'FirebaseFirestoreSwift'

  target 'TestTaskTests' do
    inherit! :search_paths
  end

  target 'TestTaskUITests' do
  end
end

将对应的Firebase Pod添加到其中,另外需要指定适应的源,否则会存在下载缓慢/失败的情况。

如果你用的不是Firebase Pod,则根据自己的需求进行调整pod的代码,另外上面的TestTask为我本地的测试项目名称,需要根据自己的项目名称来调整。

清理缓存并更新仓库

在进行上述修改后,建议清理CocoaPods缓存并更新仓库:

pod cache clean --all
pod repo update

安装Pods

执行Pods安装命令

pod install 安装过程输出如下:

TestTask % pod install
Analyzing dependencies
Downloading dependencies
Generating Pods project
Integrating client project
Pod installation complete! There are 4 dependencies from the Podfile and 20 total pods installed.

完成Pods安装

完结撒花🎉🎉🎉

注意:如果执行pod install时报错并输出

ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target Support Files/Pods-TestTask-TestTaskUITests/Pods-TestTask-TestTaskUITests.debug.xcconfig

类似错误,请见踩坑问题2,调整ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES构建设置

踩坑问题

1、不能直接使用trunk这个名称来添加镜像源,在执行pod repo add trunk时,会报下述错误:

TestTask % pod repo add trunk 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git'
[!] Repo name `trunk` is reserved for CocoaPods' main spec repo accessed via CDN.

这意味着trunk 名称是CocoaPods主仓库通过CDN访问的保留名称。我们不能直接使用trunk这个名称来添加镜像源。以下是正确的方式来添加和使用CocoaPods的镜像源:

1、移除现有的trunk源(如果已经存在):

pod repo remove trunk

2、添加镜像源,并将其命名为不同于trunk的名称:

pod repo add TsinghuaSpecs 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git'

2、TestTaskUITests 目标覆盖了 ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES 的构建设置问题:

[!] The `TestTaskUITests [Debug]` target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target Support Files/Pods-TestTask-TestTaskUITests/Pods-TestTask-TestTaskUITests.debug.xcconfig'. This can lead to problems with the CocoaPods installation
    - Use the `$(inherited)` flag, or
    - Remove the build settings from the target.

[!] The `TestTaskUITests [Release]` target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target Support Files/Pods-TestTask-TestTaskUITests/Pods-TestTask-TestTaskUITests.release.xcconfig'. This can lead to problems with the CocoaPods installation
    - Use the `$(inherited)` flag, or
    - Remove the build settings from the target.

如在执行pod install命令时,提示上述报错则表示TestTaskUITests 目标覆盖了 ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES 的构建设置,而这个设置在CocoaPods的 .xcconfig 文件中已经定义。这样可能会导致CocoaPods安装的问题。

解决方案为:

在Xcode的Build Settings中,确保将 ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES 的值设置为 $(inherited),这样就可以继承CocoaPods配置的值。

具体操作步骤为:打开Xcode项目,点击左上角左侧蓝色图标的项目名,点击右侧配置文件的Build Settings – TestTaskUITests – Build Options – ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES,这个值默认为Yes。

点击默认值,会弹出其他选项,然后点击Other

输入框默认展示YES

然后将其改为

$(inherited)

设置后的效果如下:

然后,重新执行pod install命令即可完成安装。

如果您认为这篇文章给您带来了帮助,您可以在此通过支付宝或者微信打赏网站开放者。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注