notes

近段时间再给公司APP做重构,需要创建私有库,所以牵扯到cocoapod私有库的搭建问题。

在cocoapods上搭建私有库需要创建另外一个git仓库去维护里面所有的私有库的版本,比如这个仓库取名为specs.git。这个specs.git维护的是你所有的其他私有库的版本号对应的podspec文件。

目录如下所示

specs文件夹结构

创建管理私有库的仓库

在服务器端创建私有库 注意:创建git仓库必须用git账号登录创建才能有效,如果用root在某些情况下会显示权限错误。

git init --bare yourRepoName.git
chown -R git:git yourRepoName.git

创建完成后回到本地终端,执行

pod repo add yourRepoName git@xxx.xxx.xxx.xxx:/yourRepoName.git

执行成功后,去~/.cocoapod/repos/目录下会发现多了一个文件夹\n\n

这里的ll-specs只是我自己仓库的名字,实际文件夹名取决于你的yourRepoName。

创建私有库

使用命令pod lib create yourPodName来创建私有库项目。期间需要回答几个问题。

创建完成后有一个Example文件夹和私有库文件夹,把项目文件放到私有库文件夹中的class文件夹内。

然后打开Example项目,执行pod update。这样,私有库项目文件就导入到example项目中了,可以在example项目中调试私有库程序。

编写podspec文件

项目的根目录中,有一个podspec文件。

Pod::Spec.new do |s|
# 项目名称
s.name             = 'yourPodName'
# 版本号
s.version          = '0.1.0'
# 项目摘要
s.summary          = 'A summary.'
# 项目描述
s.description      = <<-DESC
A description.
DESC
# 主页,这里要填写可以访问到的地址,不然验证不通过
s.homepage         = 'http://github/xxx'
s.license          = { :type => 'MIT', :file => 'LICENSE' }
# 作者信息
s.author           = { 'author' => 'author@email.com' }
# 项目地址,这里不建议用ssh的地址,会有警告,建议HTTP和HTTPS,最好使用HTTPS
s.source           = { :git => 'git@xxx.xxx.xxx.xxx:/yourPodName.git', :tag => s.version.to_s }
# 最低支持的iOS版本
s.ios.deployment_target = '8.0'
# 代码源文件地址
s.source_files = 'yourPodName/Classes/**/*'
# 依赖的framework
s.frameworks = 'UIKit', 'MapKit'
# 依赖的公共库或私有库
s.dependency 'AFNetworking', '~> 2.3'
end

把其中的s.source换成有效的地址就行了,cocoapod建议是https开头的地址。

验证podspec文件

使用pod lib lint yourPodName.podspec命令进行本地验证文件有效性。 使用pod spec lint yourPodName.podspec命令进行本地和远程验证文件有效性。

把私有库添加到cocoapod中

执行pod repo push yourRepoName yourPodName.podspec命令把私有库添加到私有repo中。

这个命令还会验证一次podspec文件的有效性,成功后可以在 ~/.cocoapods/repos/yourRepoName/路径中看到私有库已经被添加进yourRepoName文件夹了。如果有警告则加上--allow-warnings

团队中使用私有库

把你的管理私有库的仓库的git地址发个同事,让他们执行下面语句。

pod repo add yourRepoName git@xxx.xxx.xxx.xxx:/yourRepoName.git

在Podfile中第一行明确指定私有库的源和Github源

source 'http://xxx.xxx.xxx.xxx/yourRepoName.git'
source 'https://github.com/CocoaPods/Specs.git'

pod lib/spec lint相关参数

#####–sources 当你的.podspec文件依赖其他私有库时要引入source #####–allow-warnings 在pod lib lint验证podspec的时候,忽略警告。 #####–only-errors 在pod lib lint验证podspec的时候,只打印错误信息,不打野警告信息。 #####–fail-fast 出现第一个错误的时候就停止 #####–use-libraries 在pod lint验证时如果用到的第三方中需要使用.a静态库文件的话,则会用到这个参数。如果不使用–use-libraries则会验证不通过。 如果依赖库中有swift库,使用–use-libraries就会报错,因为swift必须以framework方式导入。所以依赖库中有swift库的话,就不能使用这个参数。

一些坑如果

私有库中依赖私有库,则验证podspec文件的时候需要加上–sources参数,否则会出现找不到你依赖的私有库。如:

pod spec lint yourPodName.podspec --sources='git@xxx.xxx.xxx.xxx:/yourRepoName.git,https://github.com/CocoaPods/Specs'

Q:podspec验证报错:error: include of non-modular header inside framework module 'yourPodName.yourClassName' [-Werror,-Wnon-modular-include-in-framework-module] A:大意是把你这个私有库打成framework的时候,因为你的私有库中import了其他私有库中的文件,所以找不到这个文件,但是xcode编译运行都是正常的。只是验证通不过。这个时候就要在pod spec lint xxx.podspec命令后加上--use-libraries。如:

pod spec lint yourPodName.podspec --sources='git@xxx.xxx.xxx.xxx:/yourRepoName.git,https://github.com/CocoaPods/Specs' --use-libraries

这样验证就不会报错了。 如果私有库中有swift库,则不能使用--use-libraries,应该用模块化导入的方式#import <xxx/xxx.h>


Q:在podspec中依赖swift库,可能在pod验证的时候会报错

Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targets which use Swift. Use the [Edit > Convert > To Current Swift Syntax…] menu to choose a Swift version or use the Build Settings editor to configure the build setting directly.

A:升级cocoapods至v1.2.0以上并在podspec文件同级的目录下添加.swift-version文件。


Q:在OS X EI Capitan系统下Cocoapods升级失败 A:两种办法

$ mkdir -p $HOME/Software/ruby\n$ export GEM_HOME=$HOME/Software/ruby\n$ gem install cocoapods[...]
1 gem installed
$ export PATH=$PATH:$HOME/Software/ruby/bin
$ pod --version
1.2.0
sudo gem install -n /usr/local/bin cocoapods

Q:有些验证失败的错误是由于cocoapods的缓存引起的,可以试试清除pod缓存 A:

// cocoapods缓存文件夹
/Users/huangqiangqiang/Library/Caches/CocoaPods/Pods/External 

搭建私有库中需要用到的git命令

打tag

git tag '1.0.0'  
git push --tags

用push, 删除远程tag

git push origin :tagName

新建分支

git branch branchName 

删分支

git branch -d branchName