0%

Cocoapods 安装及使用

它是用 Ruby 写的,并划分为多个 gem 包。它依赖于 Ruby 环境

安装步骤:

  1. 先更新系统的 Ruby 环境。
    sudo gem update --system  # 这里请翻墙一下
    gem -v                    # 查看已安装的 Ruby 版本(最新版本:2.7.8,截止2018年11月27日)
  1. 安装 CocoPods 先下载相关资源。
    默认的镜像资源服务器被天朝给墙了。所以需要先更换源地址,然后再安装。

    • 移除原先的源

      gem sources --remove https://rubygems.org/
    • 指定新源

      gem sources -add https://gems.ruby-china.com/
      // Attention:之前的地址是 https://gems.ruby-china.org,不知道什么时候莫名改成了 https://gems.ruby-china.com 。

      相关阅读:
      Ruby China 的 RubyGems 镜像上线
      RubyGems 镜像

    • 验证新源是否替换成功

      gem sources -l
  1. 安装 Cocoapods.
    (1) $sudo gem install cocoapods
    备注:苹果系统升级 OS X EL Capitan 后改为 :
    $sudo gem install -n /usr/local/bin cocoapods
    (2) $pod setup

使用 CocoaPods 的镜像索引

第一次执行 pod setup 时,CocoaPods 会将所有项目的 Podspec 文件更新到本地的~/.cocoapods/ 目录下,该索引文件较大且更新非常缓慢。
将 CocoaPods 设置成 gitcafe 或者 occhina 镜像,执行索引更新时会快很多。

pod repo remove master
pod repo add master https://gitcafe.com/akuandev/Specs.git
pod repo update

也可以将以上代码中的 https://gitcafe.com/akuandev/Specs.git 替换成 http://git.oschina.net/akuandev/Specs.git 即可使用 occhina 上的镜像。

添加新源时报错:

➜  ~ pod repo add master https://git.coding.net/CocoaPods/Specs.git
[!] To setup the master specs repo, please run `pod setup`.
➜  ~ git clone https://git.coding.net/CocoaPods/Specs.git ~/.cocoapods/repos/master

—— 参考自:https://lamjack.github.io/2016/cocoapods-install-and-use/

搜索相关框架:

打开终端,输入以下代码:

pod search 框架名   # 如: pod search AFNetworking

链接:mac 终端命令

项目中使用:

  1. 创建 Podfile 文件。
    新建项目。在终端 CD 到项目的主文件夹 (就是包含 项目.xcodeproj 的文件)。
    输入命令:
$ pod init
$ vim Podfile
  1. 编辑 Podfile
    命令模式:i -> 输入模式 ESC -> 命令模式 shift + : -> 末行模式 wq 保存退出

    i 进入模式 ESC 退出

    <!–hexoPostRenderEscape:

    # Uncomment this line to define a global platform for your project
    platform :ios, '8.0'
    inhibit_all_warnings! # 屏蔽所有第三方框架警告

target 'ProjectName' do

Uncomment this line if you're using Swift or would like to use dynamic frameworks

use_frameworks!

Pods for ProjectName

网络库

pod 'AFNetworking'
pod 'YTKNetwork'
pod 'SDWebImage'
pod 'Reachability', '~> 3.2'

服务类

pod 'QQ_XGPush'
pod 'Bugly'

UI

pod 'Masonry'
pod 'MBProgressHUD'
pod 'MJRefresh'
pod 'EAIntroView'
pod 'SDCycleScrollView'
pod 'DZNEmptyDataSet'
pod 'XLForm'
pod 'SHSPhoneComponent'
pod 'BEMCheckBox'
pod 'SCLAlertView-Objective-C'

工具组件类

pod 'ChameleonFramework'
pod 'FDFullscreenPopGesture'
pod 'IQKeyboardManager'
pod 'YYKit'
pod 'UIAlertController+Blocks'
pod 'LBXScan/LBXNative'
pod 'LBXScan/UI'
pod 'UITableView+FDTemplateLayoutCell'

end

target 'ProjectNameTests' do
inherit! :search_paths
end

"Podfile" 52L, 999C:hexoPostRenderEscape–>

  1. 保存文件后,安装第三方库
    每次更改 Podfile 都要执行 pod update(记得 cd 到项目主文件下)
    输入命令:$pod install

  2. 安装完成,此时项目文件中会多出 .xcworkspace 文件,以后就通过它来打开项目。

More: Podfile 语法参考 (译)

常见错误:

错误 1

Error fetching http://ruby.taobao.org/:
bad response Not Found 404 (http://ruby.taobao.org/specs.4.8.gz)

解决方案:把安装流程中 gem sources -a http://ruby.taobao.org/ 改为 gem sources -a https://ruby.taobao.org/

错误 2

ERROR:  While executing gem ... (Errno::EPERM)
Operation not permitted - /usr/bin/pod

解决方案:苹果系统升级 OS X EL Capitan 后会出现的插件错误,将安装流程安装 CocoaPods 的 (1) sudo gem install cocoapods ——> 改为 sudo gem install -n /usr/local/bin cocoapods

错误 3:

ERROR:The dependency is not used in any concrete target
The dependency AFNetworking is not used in any concrete target

解决方案:

  1. 安装 cocoapods 的预览版本

    sudo gem install cocoapods --pre
  1. 修改 Podfile 格式

    <!–hexoPostRenderEscape:

    platform :ios, '8.0'

target 'MyApp' do

pod 'AFNetworking', '> 2.6'
pod 'ORStackView', '
> 3.0'

end:hexoPostRenderEscape–>

*里面的 MyApp 记得替换为自己攻城里面的target。
  1. 更新 pod

    pod install / pod update

错误 4

error: RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54

image

SSL 证书错误,解决方案参考:https://gems.ruby-china.org/

vim ~/.gemrc  # 配置该文件,关闭SSL验证

文件如下:

---
:backtrace: false
:bulk_threshold: 1000
:sources:
- https://gems.ruby-china.org/
:ssl_verify_mode: 0   # 新增此处代码
:update_sources: true
:verbose: true

错误 5

RPC failed; curl 18 transfer closed with outstanding read data remaining

解决方案,增加缓冲区内存:

git config http.postBuffer 524288000

错误 6
Xcode 9.3 下运行 pod init 报错:

Error

RuntimeError - [Xcodeproj] Unknown object version.
/usr/local/lib/ruby/gems/2.4.0/gems/xcodeproj-1.5.2/lib/xcodeproj/project.rb:217:in `initialize_from_file'
/usr/local/lib/ruby/gems/2.4.0/gems/xcodeproj-1.5.2/lib/xcodeproj/project.rb:102:in `open'
/usr/local/lib/ruby/gems/2.4.0/gems/cocoapods-1.3.1/lib/cocoapods/command/init.rb:41:in `validate!'
/usr/local/lib/ruby/gems/2.4.0/gems/claide-1.0.2/lib/claide/command.rb:333:in `run'
/usr/local/lib/ruby/gems/2.4.0/gems/cocoapods-1.3.1/lib/cocoapods/command.rb:52:in `run'
/usr/local/lib/ruby/gems/2.4.0/gems/cocoapods-1.3.1/bin/pod:55:in `<top (required)>'
/usr/local/bin/pod:23:in `load'
/usr/local/bin/pod:23:in `<main>'

解决方法:安装 Cocoapods 预览版本:

sudo gem install cocoapods --pre

参考:RuntimeError - [Xcodeproj] Unknown object version.

错误 7
pod update 报错,(Gem::GemNotFoundException):

$ pod update
Traceback (most recent call last):
    2: from /usr/local/bin/pod:23:in `<main>'
    1: from /usr/local/Cellar/ruby/2.6.0_1/lib/ruby/2.6.0/rubygems.rb:302:in `activate_bin_path'
/usr/local/Cellar/ruby/2.6.0_1/lib/ruby/2.6.0/rubygems.rb:283:in `find_spec_for_exe': can't find gem cocoapods (>= 0.a) with executable pod (Gem::GemNotFoundException)

解决方案:

# 更新 gem 版本
sudo gem update --system

# 卸载 cocoapods
gem install cocoapods

# 重新安装 cocoapods 到指定目录
sudo gem install cocoapods -n /usr/local/bin

默认情况下,安装 cocoapods 时(sudo gem install cocoapods)会被安装到 /usr/bin 目录下,但是苹果为了系统安全,该目录禁止任何写入,root 用户也不能。

参考:

  • 用 CocoaPods 做 iOS 程序的依赖管理 @唐巧 (阅读难度:★★)
  • iOS 最新版 CocoaPods 的安装流程 (阅读难度:★)
  • CocoaPods 公有库
  • Cocoapods 入门 @不会开机的男孩
  • 看一遍就会的 CocoaPods 的安装和使用教程
    概述:第三方库依赖管理工具,管理第三方库。
  • CocoaPods 安装和使用教程
  • iOS 静态库,动态库与 Framework
  • http://www.jianshu.com/p/8af475c4f717
  • http://ios.jobbole.com/90957/

欢迎关注我的其它发布渠道