> For the complete documentation index, see [llms.txt](https://philm.gitbook.io/philm-ios-wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://philm.gitbook.io/philm-ios-wiki/mei-zhou-yue-du/cocoapods.md).

# cocoaPods

## 创建方法

1.创建项目的Podspec索引文件.进入需要添加的库根目录中初始化一个Podspec文件.命令

```
cd ~/Desktop/xxxx
pod spec create xxxx //xxxx 为spec文件名,和项目名称保持一致
```

2.建立远程仓库.这里用的github,在github的页面建立xxxx目录,然后编写自己的spec文件.删除注释部分. 参数说明

| key             | 描述                      |
| --------------- | ----------------------- |
| s.name          | 声明库的名称                  |
| s.summary       | 对这个库的简短说明介绍             |
| s.version       | 当前库的版本                  |
| s.homepage      | 声明库的主页                  |
| s.license       | 所采用的授权版本                |
| s.author        | 库的作者                    |
| s.platform      | 库运行的平台和系统的最低版本          |
| s.source        | 库的仓库地址以及版本号或者committed等 |
| s.source\_files | 声明库的源代码的位置，库的真正路径       |
| s.resources     | 存放不想参与编译的资源文件           |
| s.frameworks    | 声明了库所依赖的系统核心库           |
| s.dependency    | 依赖的第三方库                 |

编写完成 提交

```
  git init
  git add -A  //添加到git得暂存区
  git commit -m "first commit" //提交到本地仓库
  git remote add origin  https://github.com/yourname/xxxx.git  //添加远端仓库地址
  git push -u origin master //把本地代码推送到远端仓库
```

3.验证Podspec文件的有效性

```
pod spec lint xxxx.podspec
```

这里可能有error 要解决error

4.提交tag,然后再次验证文件有效性.

```
  //新建一个tag
  git tag 0.0.1
  //tag推送到远端
  git push origin 0.0.1
```

5.给cocoapods添加私有repo

```
  pod repo add xxxx https://github.com/yourname/xxxx.git

  pod repo list

  pod repo push xxxx ~/Desktop/xxxx/xxxx.podspec
```

到这里就已经添加好私有库啦,但是是本地的,需要使用trunk发布代码到cocopods中. 6.注册trunk并上传

```
  pod trunk register yourname@email.com 'userName'
  pod trunk push xxxx.podspec //上传podspec文件
```

现在搜索下\~ pod search xxxx

如果其他电脑搜不到 可以使用gem source -l 看一下是不是国内外源不一样

## 使用方法

1.编写Podfile文件. 在需要使用库的文件根目录新建 Podfile文件

```
cd ~/Desktop/podDemo
touch Podfile       //新建一个名为Podfile的文件
```

编写profile

| key                     | 描述                                                                                   |
| ----------------------- | ------------------------------------------------------------------------------------ |
| platform                | 指定平台和最低版本 若不指定平台版本，官方文档里写明各平台默认值为iOS：4.3,OS X：10.6,tvOS：9.0,watchOS：2.0              |
| inhibit\_all\_warnings! | 屏蔽库里面的所有警告                                                                           |
| use\_frameworks!        | 使用frameworks动态库替换静态库链接 OC默认关闭 swift默认开启                                              |
| source                  | 指定specs的位置,自定义添加自己的podspec.注意的是cocoapods 官方source是隐式的需要的，一旦你指定了其他source 你就需要也把官方的指定上 |
| pod                     | 依赖库                                                                                  |

依赖库的基本写法：

```
pod 'xxxx', //不显式指定依赖库版本，表示每次都获取最新版本
pod 'xxxx', '0.01'//只使用0.0.1版本
pod 'xxxx', '>0.0.1' //使用高于0.0.1的版本
pod 'xxxx', '>=0.0.1' //使用大于或等于0.0.1的版本
pod 'xxxx', '<0.0.2' //使用小于0.0.2的版本
pod 'xxxx', '<=0.0.2' //使用小于或等于0.0.2的版本
pod 'xxxx', '~>0.0.1' //使用大于等于0.0.1但小于0.1的版本，相当于>=0.0.1&&<0.1
pod 'xxxx', '~>0.1' //使用大于等于0.1但小于1.0的版本
pod 'xxxx', '~>0' //高于0的版本，写这个限制和什么都不写是一个效果，都表示使用最新版本
```

依赖库的自定义写法

```
使用本地文件
pod 'xxxx', :path => '~/Desktop/xxxx'

引用仓库根目录的podspec
使用仓库中的master分支:
pod 'xxxx', :git => 'https://github.com/.git'

使用仓库的其他分支:
pod 'xxxx', :git => 'https://github.com/yourName/xxxx.git' :branch => 'release'

使用仓库的某个tag:
pod 'xxxx', :git => 'https://github.com/yourName/xxxx.git', :tag => '0.0.1'
```

```
cd ~/Desktop/podDemo
pod install
```

## 常见问题 <http://www.jianshu.com/p/6e5c0f78200a>
