Cocoapods 创建私有库
一、创建pod 私有仓库
目的:此用于保存podspec文件。podspec文件是用来描述要开源的代码的一些信息,比如名称、版本、要公开的源码文件、资源文件、依赖的其他的库等等。
在github创建一个仓库即可作为pod的私有仓库。
二、添加pod私有仓库
pod repo add REPO_NAME SOURCE_URL
查看私有仓库是否添加成功
cd ~/.cocoapods/repos/REPO_NAME
pod repo lint .
删除私有仓库
pod repo remove REPO_NAME
然后开始写代码。
三、添加podspec文件到仓库中。
进行开源或者共享的时候,核心就是要编写 podspec文件。
生成podspec文件模版
pod spec create '项目名称'
如果还没有创建项目,可以用这个命令在创建时配置信息,创建的同时就能生成podspec文件
pod lib create '项目名称'
通过这个命令Cocoapods
会给你创建好,Podspec
文件,travis文件,MIT Lisence文件以及README.md
文件等。
根据模版,修改 podspec文件
Pod::Spec.new do |s|
s.name = "Artsy+OSSUIFonts"
s.version = "1.1.1"
s.summary = "The open source fonts for Artsy apps + UIFont categories."
s.homepage = "https://github.com/artsy/Artsy-OSSUIFonts"
s.license = 'Code is MIT, then custom font licenses.'
s.author = { "Orta" => "orta.therox@gmail.com" }
s.source = { :git => "https://github.com/artsy/Artsy-OSSUIFonts.git", :tag => s.version }
s.social_media_url = 'https://twitter.com/artsy'
s.platform = :ios, '7.0'
s.requires_arc = true
s.source_files = 'Pod/Classes'
s.resources = 'Pod/Assets/*'
s.frameworks = 'UIKit', 'CoreText'
s.module_name = 'Artsy_UIFonts'
end
不过针对其中source_file
、resource
以及license
稍微说两句。对于source_file
主要是指你那些目录是可以被包含进来的,而resource
是指哪些文件是作为资源文件进行引入的。而对于这些文件的描述都是可以用一些匹配公式的。
需要主要的是,source的地址在git上,执行了tag等于 s.version号。因此在提交代码发版时要打tag才可以。
子模块编写
s.subspec 'Security' do |ss|
ss.source_files = 'AFNetworking/AFSecurityPolicy.{h,m}'
ss.public_header_files = 'AFNetworking/AFSecurityPolicy.h'
ss.frameworks = 'Security'
end
只需通过父节点名.subspec '子模块名' do |子节点名|
来进行声明一个子模块,而子模块里面一般都只需要配置一些文件匹配的信息、资源文件的信息以及一些依赖的信息即可。
四、将podspec文件推送到私有pod仓库
在提交之前,要进行本地验证
本地验证:
pod lib lint SPEC_NAME.podspec
可选参数:
--verbose : 显示详细信息
--allow-warnings: 是否允许警告,用到第三方框架时,用这个参数可以屏蔽讲稿
--fail-fast: 在出现第一个错误时就停止
--use-libraries:如果用到的第三方库需要使用库文件的话,会用到这个参数
--sources:如果一个库的podspec包含除了cocoapods仓库以外的其他库的引用,则需要改参数指明,用逗号分隔。
--subspec=Name:用来校验某个子模块的情况。
注意:如果库用到了第三方的话要带上 --use-libraries,否则会报错,上传不上去。
如果出现这种报错
ERROR | [iOS] unknown: Encountered an unknown error (/usr/bin/xcrun simctl list -j devices
xcrun: error: unable to find utility "simctl", not a developer tool or in PATH) during validation.
则说明需要在Xcode的偏好设置中,配置好命令行工具。
如果出现这种报错
'/Users/hope/Library/Developer/Xcode/DerivedData/App-cwxezutxeaegaxafsxzavvctcqdn/Build/Products/Release-iphonesimulator/CTMediator/CTMediator.framework/Headers/CTMediator.h' [-Werror,-Wnon-modular-include-in-framework-module]
则需要在项目的build settings中设置 Allow Non-modular Includes In Framework Modules 为 YES
提交命令:
pod repo push REPO_NAME SPEC_NAME.podspec
比如:
pod repo push PrivatePods B_Category.podspec --allow-warnings --verbose --use-libraries(使用了第三方库)
此命令会执行远程验证,将会执行pod spec lint
命令。
如果出现这种报错。
git pull Your configuration specifies to merge with the ref 'refs/heads/master' from the remote, but no such ref was fetched.或者The repo
xxxxx
at `../../../../../.cocoapods/repos/xxxx is not clean,尝试使用 git clean -f 没有效果。那么就可以删除repo,再重新添加即可。
使用私有仓库
在podfile顶部指定资源地址:
source 'URL_TO_REPOSITORY'
默认的,git会从 podrepo 仓库中去寻找,podfile 没有显示指明 pod 仓库,其实默认的是
source 'https://github.com/CocoaPods/Specs.git'
当我们创建了私有仓库的时候,要把创建的私有仓库的地址,加入进行,并且最好放在了前面,好让install的时候优先从我们的私有仓库查找。
source 'https://github.com/ourpods/private.git'
我们还可以直接使用本地模式,从而忽略上面的source。
pod 'SPEC_NAME', :path => SPEC_NAME_PATH
还可以直接指定 git地址。
pod 'SPEC_NAME', :git => "https://www.github.com/balabla.git"
私有库中引用私有库:
Podspec
文件中没有指明私有仓库地址的地方。所以需要在验证和上传私有库的时候进行指明。
即在下面这两条命令中进行指明:pod lib lint 项目名.podspec --sources=...
以及`pod repo push --sources=...,要不然你在检验项目以及提交项目过程中就会出现Error的情况。
podfile文件使用可以参考玩转Podfile
网友评论