CocoaPods 的安装和使用
环境搭建(最好可以在代理环境下进行,要不然会很慢或者 Time Out)
安装 Ruby 环境
安装系统需要的包
安装 Homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安装 Rails 必要的一些三方库
brew install libxml2 libxslt libiconv
安装 RVM
RVM 是一个命令行工具,可以提供一个便捷的多版本 Ruby 环境的管理和切换。
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -sSL https://get.rvm.io | bash -s stable
如果上面的连接失败,可以尝试:
curl -L https://raw.githubusercontent.com/wayneeseguin/rvm/master/binscripts/rvm-installer | bash -s stable
载入 RVM 环境:
source ~/.rvm/scripts/rvm
修改 RVM 下载 Ruby 的源,到 Ruby China 的镜像:
echo "ruby_url=https://cache.ruby-china.org/pub/ruby" > ~/.rvm/user/db
用 RVM 安装 Ruby 环境
安装一个 Ruby 版本,我们大可以按照 Ruby China 的教程:
rvm requirements
rvm install 2.3.0
但对于一个软件不升级到最新版就不舒服星人来说,需要的则是列出已知的 Ruby 版本:
rvm list known
然后在其中找到最新版然后安装:
rvm install 2.2
查询已经安装的 ruby:
rvm list
设置为默认版本:
rvm use 2.2.4 --default
卸载一个已安装不需要的版本:
rvm remove 2.0.0
安装 Bundler:
gem install bundler
安装 Rails 环境
gem install rails
安装CocoaPods
升级 gem(也可以先gem -v
检查一下版本):
sudo gem update --system
替换 ruby 软件源:
RubyGems 被墙了,所以需要换成国内的镜像资源
gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
一般使用的都是淘宝的资源https://ruby.taobao.org/
,但偶尔也会出现连接不上的情况,这时候可以换 Ruby China https://gems.ruby-china.org
试试。
验证你的 Ruby 镜像,确保只有一个源:
gem sources -l
安装 CocoaPods:
sudo gem install cocoapods
pod setup
Cocoapods 将它的信息下载到~/.cocoapods目录下时,因为所有的项目的 Podspec 文件都托管在 GitHub 上,速度会比较慢,换成国内镜像的话会快很多:
pod repo remove master
pod repo add master https://gitcafe.com/akuandev/Specs.git
pod repo update
oschina 上的地址http://git.oschina.net/akuandev/Specs.git
也可以。
CocoaPods 的使用
通过终端使用
建立 Podfile 文件
cd 到项目所在目录
vim Podfile
Podfile 语法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18source 'https://github.com/CocoaPods/Specs.git' //指定镜像仓库的源
platform :ios, '8.0' //指定所支持系统和最低版本
inhibit_all_warnings! //屏蔽所有 warning
use_frameworks! //可以用 framework 的 pod 替代静态库的
generate_bridge_support! //指定后从所有已经安装的 pod 的头文件中会生成一个 BridgeSupport 元数据文档
workspace '项目空间名' //指定项目空间名
xcodeproj '工程文件名' //指定 xcodeproj 工程文件路径
target ’Tenementer’ do //指定对应的 Target
link_with 'target名称1', 'target名称2' //链接用户工程里面的 target
pod 'Moya/RxSwift' //引入库,什么版本都可以(一般是最新版本)
pod 'Kingfisher', '0.9' //指定版本
pod 'Gloss', '< 0.7.2' //指定版本的范围:> < >= <=
pod 'Alamofire', '~> 3.4.0' //从指定版本到倒数第二位版本号升1为止,比如 '~> 3.4.0'是指 3.4.0 <= 版本 < 3.5.0 的最新版本
pod '库名', :podspec => 'podspec文件路径' //指定导入库的 podspec 文件路径
pod '库名', :git => '源码git地址' //指定导入库的源码 git 地址(branch \ tag \ commit)
pod '库名', :tag => 'tag名' //指定导入库的 Tag 分支
pod 'SnapKit', :path => '~/Documents/SnapKit' //使用本地文件
end
下载第三方库
cd 到项目所在目录
pod install
成功后使用 CocoaPods 生成的 .xcworkspace 文件来打开工程,而不是以前的 .xcodeproj 文件。
以后要更改 Podfile 文件,改为执行pod update
命令。
-
执行
pod install
或者pod update
的时候会升级 CocoaPods 的 spec 仓库,加一个参数可以省略这一步,然后速度就会提升不少。加参数的命令如下:pod install --verbose --no-repo-update pod update --verbose --no-repo-update
查找第三方库
你如果不知道 cocoaPods 管理的库中,是否有你想要的库,那么你可以通过pod search
命令进行查找。
导入头文件
选择 target(就是左边你的工程 target )—> BuildSettings —> search Paths 下的 User Header Search Paths,双击后面的空白区域,点击 “+” 号添加一项:并且输入:“$(PODS_ROOT)”,选择 recursive。
通过 Alcatraz 插件使用
安装 Alcatraz
curl -fsSL https://raw.github.com/supermarin/Alcatraz/master/Scripts/install.sh | sh
但是这个方法需要挂代理才能下载安装,而且速度也不快。也可以选择通过 GitHub https://github.com/alcatraz/Alcatraz
直接下载。
等到出现** BUILD SUCCEEDED **
就成功了。
安装 CocoaPods 插件
重启Xcode,Window -> Package Manager -> 搜索 cocoapods -> 点击 install。
再重启 Xcode,Product 看到 CocoaPods 选项,就表示成功。
创建 Podfile 文件
Product -> CocoaPods -> Creat/Edit Podfile,弹出的 Podfile 文件语法和之前的没区别。
下载第三方库
Product -> CocoaPods -> Install Pods 或者 Update Pods。最后提示 SDK 导入成功,以及 Do you want to open the workspace?,点击 Yes 后重新打开工程。
- Mac 更新最新系统后, cocoa pods 插件报错
Expanded GEM_PATH:/usr/bin
,Product -> CocoaPods -> GEM_PATH: 手动修改/usr/local/bin就好了。