分类
dev

iOS Project with Dependency Manager

Notice: 当前Apple为Swift正在准备一套包管理器,详情参见Github

依赖管理器 Dependency Manager 和 包管理器 Package Manager 

Cocoapods 依赖管理器 Dependency Manager
cocoapods.org/
官方简介:

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over eighteen thousand libraries and can help you scale your projects elegantly.

官方安装是通过gem源进行的。

但是有以下几点缺点:
gem install需要sudo权限。
默认的gem源https://rubygem.org/国内环境无法访问。淘宝提供的gem镜像也并不稳定。

使用Homebrew包管理器安装

关于Homebrew:(如果已用,请跳过此节)
brew.sh
官方的一点介绍:

Homebrew installs the stuff you need that Apple didn’t.

Homebrew installs packages to their own directory and then symlinks their files into /usr/local.

Homebrew won’t install files outside its prefix, and you can place a Homebrew installation wherever you like.

Trivially create your own Homebrew packages.

It's all git and ruby underneath, so hack away with the knowledge that you can easily revert your modifications and merge upstream updates.

Homebrew formulae are simple Ruby scripts

Homebrew complements OS X. Install your gems with gem, and their dependencies with brew.

强烈推荐Mac环境下使用Homebrew包管理器,而非MacPorts。
Homebrew已有的formula参见:
https://github.com/Homebrew/homebrew-core/tree/master/Formula

安装Homebrew:

Terminal(终端)中粘贴并运行此命令:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

以后需要查找包,直接使用搜索命令:
brew search …

安装包:
brew install …

检查Homebrew状态:
brew doctor

这里我们将使用Homebrew来安装Cocoapods。
brew search cocoapods
可以看到结果里有Cocoapods
那直接brew install cocoapods
等待安装完成~

测试一下Cocoapods是否安装成功,运行命令:
pod
返回结果里有pod正确的使用方法解释,看来是安装成功了。
以后pod升级也不用再sudo gem install了。

使用官方gem安装方法的请注意了:
Cocoapods安装需要sudo权限。但是平时pod时一定不要用sudo。
特别是第一次初始化pod repos的时候,如果使用sudo,将会导致一系列问题,(其实都是权限不对导致)。

使用brew install的,本身不用sudo,不用担心这个问题。

关于Spec repos
初次安装Cocoapods将会克隆一份spec repository到本地。
因为该项目又大文件夹又多,可能容易中断。

实际Spec项目就在Github,链接:
https://github.com/CocoaPods/Specs.git
可以git clone该项目到本地,或者下载zip文件的方式获取它。

将整个repo命名成master,然后放到~/.cocoapods/repos/下即可。

这个时候,再跑一遍命令以完成Cocoapods的初始化:
pod setup

来替代Spec仓库初始化命令的操作。

使用Cocoapods控制第三方库依赖

在Xcode Project目录下,运行pod init命令,将初始化一个Podfile文件。

向其中添加pod库即可。

修改完成后,运行pod install命令,将依据此Podfile生成工程 workspace,包含配置好的所有库。

命令都会输出应有的操作提示。

Where To Go

关于该依赖管理器,还有很多运用,比如自建私有的Pods仓库,本地引用,透过Git引用等。

骆昱
2016-07-05