CocoaPods-安装、更新、报错等使用总结

CocoaPods-安装、更新、报错等使用总结

CocoaPods简介:

CocoaPods是一个用Ruby写的、负责管理iOS项目中第三方开源库的工具,CocoaPods能让我们集中的、统一管理第三方开源库,为我们节省设置和更新第三方开源库的时间。

CocoaPods安装:

因为Mac电脑自带Ruby环境,我们就只需打开终端开始动手。然而又因为默认情况下我们mac系统自带的Ruby环境版本比较低(大概是2.0.0或者其他较高版本),但是现在安装CocoaPods需要2.2.2版本及以上的,所以我们不管三七二十一先直接先升级ruby。

  1. 更新Ruby

  2. 安装CocoaPodssudo gem install -n /usr/local/bin cocoapods:可能需要输入电脑密码:

    执行结果:

    Password:
    Fetching claide-1.0.3.gem
    Fetching CFPropertyList-3.0.4.gem
    Fetching atomos-0.1.3.gem
    Fetching xcodeproj-1.21.0.gem
    Fetching ruby-macho-2.5.1.gem
    Fetching nanaimo-0.3.0.gem
    Fetching colored2-3.1.2.gem
    Fetching nap-1.1.0.gem
    Fetching molinillo-0.8.0.gem
    Fetching gh_inspector-1.1.3.gem
    Fetching fourflusher-2.3.1.gem
    Fetching escape-0.0.4.gem
    Fetching cocoapods-try-1.2.0.gem
    Fetching netrc-0.11.0.gem
    Fetching cocoapods-trunk-1.6.0.gem
    Fetching cocoapods-search-1.0.1.gem
    Fetching cocoapods-plugins-1.0.0.gem
    Fetching cocoapods-downloader-1.5.1.gem
    Fetching cocoapods-deintegrate-1.0.5.gem
    Fetching ffi-1.15.4.gem
    Fetching ethon-0.15.0.gem
    Fetching typhoeus-1.4.0.gem
    Fetching public_suffix-4.0.6.gem
    Fetching fuzzy_match-2.0.4.gem
    Fetching concurrent-ruby-1.1.9.gem
    Fetching httpclient-2.8.3.gem
    Fetching algoliasearch-1.27.5.gem
    Fetching addressable-2.8.0.gem
    Fetching zeitwerk-2.4.2.gem
    Fetching tzinfo-2.0.4.gem
    Fetching i18n-1.8.10.gem
    Fetching activesupport-6.1.4.1.gem
    Fetching cocoapods-1.11.2.gem
    Fetching cocoapods-core-1.11.2.gem
    Successfully installed nanaimo-0.3.0
    Successfully installed colored2-3.1.2
    Successfully installed claide-1.0.3
    ...
    ...
    ...
    Parsing documentation for activesupport-6.1.4.1
    Installing ri documentation for activesupport-6.1.4.1
    Parsing documentation for cocoapods-core-1.11.2
    Installing ri documentation for cocoapods-core-1.11.2
    Parsing documentation for cocoapods-1.11.2
    Installing ri documentation for cocoapods-1.11.2
    Done installing documentation for nanaimo, colored2, claide, CFPropertyList, atomos, xcodeproj, ruby-macho, nap, molinillo, gh_inspector, fourflusher, escape, cocoapods-try, netrc, cocoapods-trunk, cocoapods-search, cocoapods-plugins, cocoapods-downloader, cocoapods-deintegrate, ffi, ethon, typhoeus, public_suffix, fuzzy_match, concurrent-ruby, httpclient, algoliasearch, addressable, zeitwerk, tzinfo, i18n, activesupport, cocoapods-core, cocoapods after 12 seconds
    34 gems installed
    
  3. 如果安装了多个Xcode使用下面的命令选择(一般需要选择最近的Xcode版本):sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

  4. 安装本地库:pod setup

    执行结果:

    Setting up CocoaPods master repo
      $ /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress
      Cloning into 'master'...
      remote: Counting objects: 1879515, done.        
      remote: Compressing objects: 100% (321/321), done.        
      Receiving objects:  21% (404525/1879515), 73.70 MiB | 22.00 KiB/
    

    然后就是漫长的等待,当然,网络好的情况下会更快。

    说明:该步骤是安装本地库,不安装也没事,项目可以照常pod install。

    只不过不安装的话就不能使用pod search 命令了;如果你追求完美那就继续往下看;

    最新版的MacOS Catalina 及以后的系统命令行执行pod setup命令后直接结束啦;

    需要手动安装本地库:

    git clone https://github.com/CocoaPods/Specs.git ~/.cocoapods/repos/trunk
    

    目录结构见下图,如果不是就自己手动移动一下:

CocoaPods版本升级

  1. 执行命令:gem -v,查看当前RubyGems版本;

  2. 执行命令:gem sources -l,查看RubyGems源,确保只有 gems.ruby-china.com

    执行结果:

    *** CURRENT SOURCES ***
    
    https://gems.ruby-china.com
    
  3. 执行命令:gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/,修改RubyGems源:删除rubygems.org源,添加gems.ruby-china.com源;

  4. 执行命令:gem update --system,如果需要管理员权限就执行:sudo gem update --system,更新RubyGems版本,可能需要FQ;

  5. 执行命令:pod --version,查看自己的CocoaPods版本号;

    执行结果:1.8.4

  6. 执行命令:sudo gem install cocoapods,安装或者更新CocoaPods

  7. 输入电脑登录密码;

    执行结果:

    .....
    .....
    Installing ri documentation for cocoapods-1.9.1
    Done installing documentation for ffi, ethon, typhoeus, cocoapods-core, xcodeproj, cocoapods after 33 seconds
    6 gems installed
    
    
  8. 执行命令:pod --version,查看下CocoaPods版本号是否更新;

到此,CocoaPods升级完成了。

CocoaPods常用语法

// 安装最新版cocoapods
sudo gem install cocoapods --pre

// 移除本地master
sudo rm -fr ~/.cocoapods/repos/master

// 移除本地缓存
sudo rm -fr ~/Library/Caches/CocoaPods/

// 重新setup,如果很慢可使用问题1的解决方法(git clone)
pod setup --verbose

// 移除trunk
pod repo remove trunk

// 搜索
pod search AFNetworking

// 创建Podfile文件:
pod init

// 打开Podfile文件:
open Podfile

// 添加第三方Pod库:
pod 'Alamofire'

// 开始下载添加的所有的第三方Pod:
pod install

CocoaPods错误处理

  1. cocoapods-1.8.4版本错误:

    1. 错误内容:

      
      [!] CDN: trunk Repo update failed - 7 error(s):
      CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/0/f/b/MJRefresh/1.3.5/MJRefresh.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443)
      CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/0/f/b/MJRefresh/2.4.1/MJRefresh.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443)
      CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/0/f/b/MJRefresh/2.4.11/MJRefresh.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443)
      CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/0/f/b/MJRefresh/3.0.6/MJRefresh.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443)
      CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/0/f/b/MJRefresh/3.1.0/MJRefresh.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443)
      CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/0/f/b/MJRefresh/3.1.12/MJRefresh.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443)
      CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/0/f/b/MJRefresh/3.1.15.3/MJRefresh.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443)
      
      
    2. 解决办法:终端运行:pod repo remove trunk

posted @ 2019-12-03 10:21  略略略~  阅读(3306)  评论(0编辑  收藏  举报