CocoaPods
是OS X和iOS下的一个第三方开源类库管理工具,经过CocoaPods
工具咱们能够为项目添加依赖库(这些类库必须是CocoaPods
自己所支持的),而且能够轻松管理其版本。它是目前iOS开发中使用最普遍的开源库管理工具,若是咱们内部协做的组件化可以使用这种方式管理的话,那将是很便利的。 在经过Cocoapods
创建内部私有库以前,咱们须要再熟悉下Cocoapods
的工做流程,咱们建立内部私有库时也会依照这个流程来。ios
本文目录git
1、Cocoapods的工做流程github
2、创建Cocoapods私有库json
3、使用私有库swift
4、问题总结bash
工做流程如图所示: 框架
这里存放了各个框架的描述文件,托管在github上: CocoaPods/Specs工具
在安装cocoapods
时,执行的pod setup
就是讲远程索引克隆到本地,本地索引的目录为:组件化
~/.cocoapods/repos/master
复制代码
本地索引和远程索引的目录一致,结构以下: 测试
每一个库的每一个版本都对应一个json格式的描述文件:
{
"name": "YYImage",
"summary": "Image framework for iOS to display/encode/decode animated WebP, APNG, GIF, and more.",
"version": "1.0",
"license": {
"type": "MIT",
"file": "LICENSE"
},
"authors": {
"ibireme": "ibireme@gmail.com"
},
"social_media_url": "http://blog.ibireme.com",
"homepage": "https://github.com/ibireme/YYImage",
"platforms": {
"ios": "6.0"
},
"source": {
"git": "https://github.com/ibireme/YYImage.git",
"tag": "1.0"
},
"requires_arc": true,
"default_subspecs": "Core",
"subspecs": [
{
"name": "Core",
"source_files": "YYImage/*.{h,m}",
"public_header_files": "YYImage/*.{h}",
"libraries": "z",
"frameworks": [
"UIKit",
"CoreFoundation",
"QuartzCore",
"AssetsLibrary",
"ImageIO",
"Accelerate",
"MobileCoreServices"
]
},
{
"name": "WebP",
"dependencies": {
"YYImage/Core": []
},
"ios": {
"vendored_frameworks": "Vendor/WebP.framework"
}
}
]
}
复制代码
当执行pod search命令时,若是本地索引不存在,就会建立出来:
$ pod search afn
Creating search index for spec repo 'master'..
复制代码
本地索引文件路径为:
~/Library/Cache/Cocoapods/Pods
复制代码
以YYImage
为例,它的远程框架库就是json文件中的source: github.com/ibireme/YYI…
因此再用文字总结下Cocoapods
工做流程大概就是:
一、本地安装cocoapods,创建本地索引库和远程索引库的映射
二、本地项目pod install
三、查找本地索引文件,而后找到各个库对应版本的json文件
四、经过json文件source字段找到引用库的git地址
五、把库文件拉到本地项目
建议采用framework
的形式建立私有库,这能够很好的在开发阶段检查出库的不兼容或者文件权限出现的问题,Swift编写的代码经过Cocoapods
生成的都是framework
。
如何创建远程索引库 首先咱们须要创建一个内部的远程索引库,相似Cocoapods/Spec
的功能,以后添加的库文件索引文件都会存放到这里:username@bitbucket.org/sealcn/seal… 创建本地和远程索引仓库的关联:
pod repo add SealRepo https://username@bitbucket.org/sealcn/sealrepo.git
复制代码
执行pod repo
能够看到咱们有了两个索引仓库,能够去在这个目录~/.cocoapods/repos
看到咱们刚创建的SealRepo
。
如何组织文件结构 咱们能够看下Alamofire的文件组织结构:
咱们看到这几个文件:
Source
用于存放Framework源文件,Example
用于放Demo项目docs
和Documentation
放说明文档,这个是可选的,Tests
测试文件也是可选。 咱们制做私有库时会仿照这个格式。由于是Swift的工程,接口的开放直接经过open、public等关键字指定,因此工程中的ABTest.h头文件能够删除,加入咱们本身的库文件。
public
或者
open
关键字。
经过Xcode菜单栏File->New->Target...
添加一个Example工程。 引入第三方库 若是无第三库引用能够跳过这一步。 注意:引入Podfile文件,须要framework和Example两个target都添加上。 测试项目 须要先编译framework,没有问题以后,导入到Demo项目里
import ABTest
复制代码
运行Dome,测试开发功能有没有问题。
push项目到远程库 若是已经关联过远程私有仓库,这一步能够跳过。 在远程配置一个git地址,而后将本地项目关联到远程私有仓库:
git remote add origin 仓库地址
复制代码
如过是首次关联远程仓库,在push以前咱们通常须要先拉去远程分支
git pull origin master
复制代码
若是提示:
There is no tracking information for the current branch.
复制代码
那是由于本地库和远程库没有创建联系,git认为这两个仓库可能不是同一个,若是咱们确认对应库没问题,可使用:
$ git pull origin master --allow-unrelated-histories
复制代码
将远程库文件强制拉到本地仓库。 以后再执行push命令将项目推到远程仓库。
git push -u origin master
复制代码
一、添加.swift-version .swift-version
文件用来告诉cocoapods
当前文件swift的版本,用命令行创建:
$ echo "4.0" > .swift-version
复制代码
二、添加LICENSE 每一个使用cocoapods
添加的库都须要准守开源协议,通常是MIT
协议,由于bitbucket无法自动生成,咱们能够手动生成这个同名文件,而后把协议内容复制进去:
MIT License
Copyright (c) [year] [fullname]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
复制代码
三、建立库描述文件 能够经过命令行生成描述文件:
$ pod spec create ABTest
复制代码
而后咱们编辑ABTest.podspec文件,能够仿照下面的写法
Pod::Spec.new do |s|
s.name = "ABTest"
s.version = "0.0.1"
s.summary = "ABTest with Firebase"
s.description = "This is a ABTest Framworks on swift"
s.homepage = "https://bitbucket.org/company/abtest/src/master/"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "username" => "username@company.biz" }
# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
s.platform = :ios, "8.0"
# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
s.source = { :git => "git@bitbucket.org:company/mvabtest.git", :tag => s.version }
s.source_files = "Source", "Source/*.swift"
# ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
# s.resource = "icon.png"
# s.resources = "Resources/*.png"
# ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
s.requires_arc = true
s.static_framework = true
s.dependency "Firebase/Core"
s.dependency "Firebase/RemoteConfig"
#s.ios.vendored_frameworks = "ABTest.framework"
s.xcconfig = { 'SWIFT_INCLUDE_PATHS' => '$(PODS_ROOT)/Firebase/CoreOnly/Sources' }
end
复制代码
此时咱们的文件目录看起来应该大概是这个样子:
四、验证本地podspec
文件
pod lib lint
复制代码
该命令用于检查podspec文件书写是否正确,若是有error须要解决,warning能够不用管(可能会遇到较多问题,需耐心解决0。0)。解决以后再次运行检查命令,当命令行显示:
-> ABTest (0.0.1)
ABTest passed validation.
复制代码
说明咱们本地配置成功了,到这里本地的第一个版本就算完成了! 而后咱们须要将本次修改提交打上tag,提交到远程仓库。
git add .
git commit -m "build v0.0.1"
git push origin master
git tag 0.0.1
git push --tags
复制代码
五、验证远程索引文件 上传代码成功以后,咱们须要再次验证它跟远程仓库(ABTest远程库和.podspec)是否匹配正确,执行:
pod spec lint
复制代码
当出现:
ABTest.podspec passed validation
复制代码
时,说明咱们远程仓库匹配正确。
六、提交podspec文件到SpecsRepo
$ pod repo push SealRepo ABTest.podspec
复制代码
这个命令会包含pod spec lint
命令,验证经过以后,会添加.podspec文件到本地索引库:
和远程索引库:
咱们能够像使用其余库文件同样在Podfile文件中添加使用私有库了,引入方法有两种:
一、全局添加 在Podfile文件最上面添加一行:
source 'git@bitbucket.org:company/sealrepo.git'
复制代码
注意:若是私有仓库和cocoapods仓库出现同名库,会出现不可预期的状况(随机拉下来公有库或者私有库文件)。这时咱们须要使用单独添加的方式。
二、单独添加
pod 'ABTest', :git => 'git@bitbucket.org:company/mvabtest.git'
复制代码
使用时经过import方法导入库就能够了。
当咱们须要升级私有库,添加或者修改方法时,只须要:
一、修改.podspec
文件中s.version
的版本号
二、提交本地修改至远程,打上对应tag
三、使用项目的工程执行pod update
一、pod search 查不到本地库 这个多是cocoadpods自己问题,pod install安装没有问题
二、更新了版本,可是pod update没有找到 咱们能够采用以下形式,手动指定版本号:
pod 'ABTest', :git => 'git@bitbucket.org:sealcn/mvabtest.git', :tag => '0.0.4'
复制代码
三、提示The 'Pods-App' target has transitive dependencies that include static binaries
这是由于引入的库被编译成了静态库,咱们能够在.podspec
文件中加入:
s.static_framework = true
复制代码
四、引入的第三方库,在pod lint
时提示找不到 能够手动指定pod目录,将firsebase替换成你的库文件路径:
s.xcconfig = { 'SWIFT_INCLUDE_PATHS' => '$(PODS_ROOT)/Firebase/CoreOnly/Sources' }
复制代码
五、提示source_files
对应文件为空 每次pod lint
时都是根据版本号进行查找的,能够检查下当前修改跟版本号是否对应。
六、Encountered an unknown error (Unable to find a specification for MVNetRequest
depended upon by MVABTest
在制做MVABTest
仓库时,引用了私有库MVNetRequest
。lint在对引用库验证时,默认只验证官网的仓库,咱们须要手动添加验证源才能经过。
pod spec lint --sources=git@bitbucket.org:sealcn/sealrepo.git,https://github.com/CocoaPods/Specs --allow-warnings
复制代码