Cocoapods 的安装与使用

官网:https://cocoapods.org/

建议科学上网,若采用国内镜像源,可能由于镜像源不同步 / 不完整 / 依赖关系冲突,导致报错

  • 1、通过 Gem 安装 Cocoapods

    详见Ruby & Gem 的安装与使用.md

  • 2、查看版本,命令如下

    1
    pod --version
  • 终端输出如下
    1
    1.15.2
  • 3、在目标项目中创建 Podfile 文件,命令如下

    1
    pod init
  • 4、添加准备集成的第三方库到 Podfile 文件,编辑如下

    【以 Alamofire 为例】
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # Uncomment the next line to define a global platform for your project
    platform :ios, '12.0'

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

    # Pods for JQDemo
    pod 'Alamofire'

    end
  • 5、安装第三方库,命令如下

    1
    pod install
  • 终端报错如下
    【网络问题 1,建议科学上网】
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Analyzing dependencies
    Adding spec repo `trunk` with CDN `https://cdn.cocoapods.org/`
    Downloading dependencies
    Installing Alamofire (5.9.0)

    [!] Error installing Alamofire
    [!] /usr/bin/git clone https://github.com/Alamofire/Alamofire.git /var/folders/yl/9m95g2tj2wd2dwvnc4mzt61m0000gn/T/d20240322-47781-w2w99u --template= --single-branch --depth 1 --branch 5.9.0

    Cloning into '/var/folders/yl/9m95g2tj2wd2dwvnc4mzt61m0000gn/T/d20240322-47781-w2w99u'...
    fatal: unable to access 'https://github.com/Alamofire/Alamofire.git/': Failure when receiving data from the peer
    【网络问题 2,建议科学上网】
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Analyzing dependencies
    Downloading dependencies
    Installing Alamofire (5.9.0)

    [!] Error installing Alamofire
    [!] /usr/bin/git clone https://github.com/Alamofire/Alamofire.git /var/folders/yl/9m95g2tj2wd2dwvnc4mzt61m0000gn/T/d20240322-47799-vs5a1b --template= --single-branch --depth 1 --branch 5.9.0

    Cloning into '/var/folders/yl/9m95g2tj2wd2dwvnc4mzt61m0000gn/T/d20240322-47799-vs5a1b'...
    fatal: unable to access 'https://github.com/Alamofire/Alamofire.git/': Failed to connect to github.com port 443 after 43636 ms: Couldn't connect to server
  • 终端输出如下

    1
    2
    3
    4
    5
    6
    7
    8
    Analyzing dependencies
    Downloading dependencies
    Installing Alamofire (5.9.0)
    Generating Pods project
    Integrating client project

    [!] Please close any current Xcode sessions and use `JQDemo.xcworkspace` for this project from now on.
    Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
  • 6、打开 .xcworkspace 文件,使用第三方库

  • 7、使用镜像【TODO:待补充】

仅供参考