1.cocoapods是什么
cocoapods(官方文档)是objective C代码的依赖管理工具,或者也可以叫做是中央代码仓库。
2. cocoapods安装和使用简介(略略略)
3.cocoapods的工作流程简介
3.1搜索开源组件库(镜像索引)
命令:$ pod search ASIHttpRequest
如何搜索开源组件?
所有的项目的 Podspec 文件都托管在https://github.com/CocoaPods/Specs。第一次执行pod setup
时,CocoaPods 会将这些podspec索引文件更新到本地的~/.cocoapods/
目录下,这个索引文件比较大,有 80M 左右。所以第一次更新时非常慢。
当你pod search 一个内容时,就在这个本地目录下进行搜索。
~/.cocoapods/
目录如下图:
3.2 下载源代码到本地
pod install --no-repo-update //忽略更新本地中心仓库
①Cocoapods会预先从github上把代码拉下来放到缓存里,缓存的目录~/Library/Caches/Cocoapods
,知道这个是有作用的,方便清理缓存。
②下载到缓存目录后把代码拷贝的你工程目录下的对应目录下:如$projectPath/Pods/ASIHTTPRequest
3.3 Podfile.lock
当你执行pod install
之后,除了 Podfile 外,CocoaPods 还会生成一个名为Podfile.lock
的文件,Podfile.lock
应该加入到版本控制里面,不应该把这个文件加入到.gitignore
中。因为Podfile.lock
会锁定当前各依赖库的版本,之后如果多次执行pod install
不会更改版本,要pod update
才会改Podfile.lock
了。这样多人协作的时候,可以防止第三方库升级时造成大家各自的第三方库版本不一致。
4.创建私有podspec
4.1 配置podspec
Cocoapods是如何知道一个开源组件需要引入哪些代码,需要添加哪些framework和lib,需要哪些资源文件的呢。
这些信息都配置在podspec
文件中。如果你打开Cocoapods的本地中心仓库文件来看的话,会发现保存的都是podspec文件。首先生成一个podspec文件,在工程目录下:
pod spec create [spec_name]
如图:
podSpec怎么写?请参考Cocoapods官网上的guide
下面是我新建的podspec,写了一些注释
#
# Be sure to run `pod spec lint JJSpec.podspec' to ensure this is a
# valid spec and to remove all comments including this before submitting the spec.
#
# To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html
# To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#
Pod::Spec.new do |s|
# ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# These will help people to find your library, and whilst it
# can feel like a chore to fill in it's definitely to your advantage. The
# summary should be tweet-length, and the description more in depth.
#
s.name = "JJSpec" #名称
s.version = "0.0.1" #版本号
s.summary = "JJSpec描述" #描述
# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!
s.description = <<-DESC
DESC
s.homepage = "http://EXAMPLE/JJSpec" #描述页面
# s.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"
# ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Licensing your code is important. See http://choosealicense.com for more info.
# CocoaPods will detect a license file if there is a named LICENSE*
# Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
#
s.license = "MIT" #版权声明
# s.license = { :type => "MIT", :file => "FILE_LICENSE" }
# ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Specify the authors of the library, with email addresses. Email addresses
# of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also
# accepts just a name if you'd rather not provide an email address.
#
# Specify a social_media_url where others can refer to, for example a twitter
# profile URL.
#
s.author = { "Iris" => "[email protected]" } #作者
# Or just: s.author = "Iris"
# s.authors = { "Iris" => "[email protected]" }
# s.social_media_url = "http://twitter.com/Iris"
# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# If this Pod runs only on iOS or OS X, then specify the platform and
# the deployment target. You can optionally include the target after the platform.
#
# s.platform = :ios
# s.platform = :ios, "5.0" #支持的系统
# When using multiple platforms
# s.ios.deployment_target = "5.0"
# s.osx.deployment_target = "10.7"
# s.watchos.deployment_target = "2.0"
# s.tvos.deployment_target = "9.0"
# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Specify the location from where the source should be retrieved.
# Supports git, hg, bzr, svn and HTTP.
#
s.source = { :git => "[email protected]:leejing397/Cocoapod-JJTestSpec.git", :tag => "#{s.version}" } #源码地址
# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# CocoaPods is smart about how it includes source code. For source files
# giving a folder will include any swift, h, m, mm, c & cpp files.
# For header files it will include any header in the folder.
# Not including the public_header_files will make all headers public.
#
s.source_files = "Classes", "Classes/**/*.{h,m}"
s.exclude_files = "Classes/Exclude"
# s.public_header_files = "Classes/**/*.h"
# ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# A list of resources included with the Pod. These are copied into the
# target bundle with a build phase script. Anything else will be cleaned.
# You can preserve files from being cleaned, please don't preserve
# non-essential files like tests, examples and documentation.
# s.resource = "icon.png"
# s.resources = "Resources/*.png"
# s.preserve_paths = "FilesToSave", "MoreFilesToSave"
# ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Link your library with frameworks, or libraries. Libraries do not include
# the lib prefix of their name.
#
# s.framework = "SomeFramework" #frameworks
# s.frameworks = "SomeFramework", "AnotherFramework"
# s.library = "iconv"
# s.libraries = "iconv", "xml2" #lib
# ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# If your library depends on compiler flags you can set them in the xcconfig hash
# where they will only apply to your library. If you depend on other Podspecs
# you can include multiple dependencies to ensure it works.
# s.requires_arc = true #是否需要arc
# s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
# s.dependency "JSONKit", "~> 1.4"
end
4.2 新建git 仓库用于存放私有库的podspec
把刚才写好的podspec上传到仓库
4.3 测试pod是否能成功
输入命令pod spec lint JJSpec.podspec --verbose --use-libraries --allow-warnings
....他会一直跑,一直跑,直到出现JJSpec.podspec passed validation.
。如下图:
此处出现很多问题:
比如没有打tag啦,tag要和version保持一致
比如描述没有写啦,描述和什么写得一样啦
一直改,一直改,直到没error出现了,这就大功告成啦
4.4 创建私有pod
pod repo add XXX(如:JJSpec)https://github.com/leejing397/GeoCompass.git(这个是存私有库的地址)
4.5 将三方库添加到私有pod
pod repo push XXX XXX.podspec
4.6 将本地库更新到最新
pod repo update XXX
4.7pod search xxxSdk(如JJSpec)
4.8 另外podfile文件里面要增加"Source"
也就是这行
source 'https://github.com/CocoaPods/Specs.git'
4.9 安装私有库
pod install
PS:遇到的坑
bug:
1.sha1计算命令:openssl dgst -sha1
.zip的framework 需要添加sha1认证。否则可能会出现pod完成后,framework下载不下来的情况
2./用户/chenzhaoshen/资源库/Caches/CocoaPods/search_index.json 如果找不到repo push成功的库,把这个文件删除,重新search