Xcode框架
Cocopods 支持Objective-C和Swift的第三方库管理工具。
官方地址:https://cocoapods.org
【pod命令】
常用命令
pod init 在项目目录里创建Podfile
pod install 按Podfile配置安装创建新的项目文件.xcworkspace
pod update 按Podfile配置更新框架
pod search <name> 查找某个框架。然后将返回信息的pod开头的行复制到Podfile文件中完成配置
辅助参数
--verbose 显示详细信息(建议使用,因为有时候要等很久却不知道处理情况)
--help 获得帮助
--no-repo-update 放在install和update命令后,跳过repo更新。
【安装cocoapods】
$ sudo gem install cocoapods
如果出现不能连接到 https://rubygems.org/ 错误,说明访问不到官方的安装源,需要挂VPN访问。
没有VPN的话,可以将安装源改为 https://ruby.taobao.org 淘宝的rubygems镜像,步骤如下:
$ gem sources --add https://ruby.taobao.org --remove https://rubygems.org/ /*添加淘宝服务器的镜像安装源,移除rubygems.org安装源*/
$ gem sources -l /*显示当前安装源*/
*** CURRENT SOURCES ***
https://ruby.taobao.org/
$ sudo gem update --system /*更新gem库,可跳过这步*/
...
...
RubyGems system software updated
$ sudo gem install cocoapods -V /*安装cocoapods并查看安装详细信息,需要等待一会儿*/
【安装失败】
如果在安装cocoapods时,返回了错误:
ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted - /usr/bin/xcodeproj
用下面的命令安装(自动移路径方式,参考:https://github.com/CocoaPods/CocoaPods/issues/3692)
$ mkdir -p $HOME/Software/ruby
$ export GEM_HOME=$HOME/Software/ruby
$ gem install cocoapods
[...]
1 gem installed
$ export PATH=$PATH:$HOME/Software/ruby/bin
$ pod --version
0.37.2
然后在.profile文件中加入前面两个export:
vi ~/.profile
i
——————————————
export GEM_HOME=$HOME/Software/ruby
export PATH=$PATH:$HOME/Software/ruby/bin
——————————————
wq
【常用框架】
$ pod list /* 可获得全部框架列表 */
Objective-C
AFNetworking 常用的网络框架。
CocoaLumberjack 强大的日志框架。性能优于NSLog,另外还可配合XcodeColors插件彩色显示调试输出信息。
FMDB 方便SQLite数据库操作的框架。
Swift
Alamofire 是AFNetworking作者开发的Swift版本。
【例子】
假设当前项目是Demo,安装CocoaLumberjack框架后,再添加AFNetowrking框架,并更新。
$ cd ~/Developer/Demo/
$ ls
Demo Demo.xcodeproj DemoTests
$ pod init
$ ls
Demo Demo.xcodeproj DemoTests Podfile
$ pod search CocoaLumberjack --verbose
…
-> CocoaLumberjack (2.0.1)
A fast & simple, yet powerful & flexible logging framework for Mac and iOS.
pod 'CocoaLumberjack', '~> 2.0.1'
- Homepage: https://github.com/CocoaLumberjack/CocoaLumberjack
- Source: https://github.com/CocoaLumberjack/CocoaLumberjack.git
- Versions: 2.0.1, 2.0.0, 2.0.0-rc2, 2.0.0-rc, 2.0.0-beta4, 2.0.0-beta3,
2.0.0-beta2, 2.0.0-beta, 1.9.2, 1.9.1, 1.9.0, 1.8.1, 1.8.0, 1.7.0, 1.6.5.1,
1.6.5, 1.6.4, 1.6.3, 1.6.2, 1.6.1, 1.6, 1.3.3, 1.3.2, 1.3.1, 1.3, 1.2.3,
1.2.2, 1.2.1, 1.2, 1.1, 1.0 [master repo]
- Subspecs:
- CocoaLumberjack/Default (2.0.1)
- CocoaLumberjack/Core (2.0.1)
- CocoaLumberjack/Extensions (2.0.1)
- CocoaLumberjack/CLI (2.0.1)
…
/* 复制框架信息中pod开头的那行:『pod 'CocoaLumberjack', '~> 2.0.1’』 */
$ nano Podfile /* 用文本编辑器,粘贴到Podfile文件中 ’/
$ cat Podfile /* 结果如下 */
# Uncomment this line to define a global platform for your project
# platform :ios, '6.0'
target 'Demo' do
pod 'CocoaLumberjack', '~> 2.0.1'
end
target 'DemoTests' do
end
$ pod install --verbose /* 开始安装并显示详细的安装信息 */
Preparing
Updating local specs repositories
Updating spec repo `master`
…
Integrating client project
[!] Please close any current Xcode sessions and use `Demo.xcworkspace` for this project from now on.
…
/* 看见[!]那行话,就表示安装成功了,关闭Xcode项目,用.xcworkspace打开就可以了 */
$ open Demo.xcworkspace /* Xcode会打开.xcworkspace */
$ pod search AFNetworking
-> AFNetworking (2.5.4)
A delightful iOS and OS X networking framework.
pod 'AFNetworking', '~> 2.5.4'
…
/* 复制 pod 开头那行,用文本编辑器编辑Podfile,粘贴进去 */
$ nano Podfile
$ pod update --no-repo-update
Update all pods
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (2.5.4)
Using CocoaLumberjack (2.0.1)
Generating Pods project
Integrating client project
Sending stats
$
/* 跳过repo本地库更新,更新所有框架完成 */