Cocoapods私有库的OC版本网上已经有不少介绍了,也介绍得很好。可是发现对Swift版的介绍得很少,虽然二者差距不大,但总归仍是有不一致的地方。今天就和小伙伴们一块儿来了解如何制做一个Swift版的私有库。html
建立私有库以前,咱们先看看公有库。在Finder中打开: ~/.cocoapods/repos。能够看到目录下有 master 文件夹,这就是公有库的git仓库。ios
1.1 建立私有git仓库做为私有的Repo 这里我使用github建立远程仓库,小伙伴可执行选择本身的远程仓库。 git
按上图操做,完成远程仓库【BOTestSpec】的建立。github
1.2 执行repo 命令添加私有库Repo 打开终端,在任意目录下执行下面的命令:swift
pod repo add BOTestSpec https://github.com/Lwindy/BOTestSpec.git
复制代码
解释:
pod repo add 【私有库名称】【1.1中建立的远程仓库的git地址】
复制代码
再次打开 ~/.cocoapods/repos,若是能看到多一个 BOTestSpec 文件中,则说明建立成功。 ruby
私有Spec建立完成了,可是里面并无内容,因此咱们还须要添加组件库到私有库里,丰富咱们的私有库。bash
pod 建立组件库的命令使用能够查看官方文档:Using Pod Lib Create。ide
cd到你要保存项目的目录而后执行下面的命令:函数
pod lib create BOTestTools
复制代码
紧接着,会有一些参数须要配置: 测试
配置完成后,会自动打开建立的项目。
若是你的选择和上图的不一致,可能会生成不一样的项目。
按上图建立【BOTestTools】仓库,存放组件项目。
远程仓库建立好后,clone到本地,存放在你想要保存的目录。
将这四个文件拷贝到clone下面的文件夹下。
打开 BOTestTools本地仓库中的 Example 中的工程。
选择 BOTestTools.podspec 文件:
相关字段能够查询官方文档【Podspec Syntax Reference】。
这是我项目中podspec文件,小伙伴们能够做为参考。
Pod::Spec.new do |s|
s.name = 'BOTestTools'
s.version = '0.1.0'
s.summary = 'A short description of BOTestTools.'
s.description = <<-DESC TODO: Add long description of the pod here. DESC
s.homepage = 'https://github.com/LWindy/BOTestTools'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'LWindy' => 'windy.lin@163.com' }
s.source = { :git => 'https://github.com/LWindy/BOTestTools.git', :tag => s.version.to_s }
s.ios.deployment_target = '8.0'
s.source_files = 'BOTestTools/Classes/**/*'
end
复制代码
添加文件 Tools.swift 到 BOTestTools文件夹下的Classes文件夹中。
Tools.swift 文件中简单的给 UIView 添加一个分类。能够设置 UIView 的 corner 和 border。
为了方便链式调用,函数返回值都是Self。并且为了能访问函数,须要给函数加public。
向库中添加文件或者修改文件,都须要执行
pod update
才能够在 Example 工程中使用。
在 Main.storyboard 中给 View 添加一个 按钮。
再在 ViewController.swift 中使用 import BOTestTools
导入私有库 BOTestTools。
在 viewDidLoad
方法中,设置 按钮btn 的圆角和边框。
运行结果:
假如要向私有库中添加一张图片,须要使用 Assets来保存资源。
在Assets目录下添加 ToolsAsset.xcassets,用来存放图片。
如上图所示,添加一张测试图片进入Assets中。
同时,因为添加了资源,因此须要更新 podspec 文件,增长 s.resource_bundles 字段。
Pod::Spec.new do |s|
s.name = 'BOTestTools'
s.version = '0.1.0'
s.summary = 'A short description of BOTestTools.'
s.description = <<-DESC TODO: Add long description of the pod here. DESC
s.homepage = 'https://github.com/LWindy/BOTestTools'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'LWindy' => 'windy.lin@163.com' }
s.source = { :git => 'https://github.com/LWindy/BOTestTools.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'BOTestTools/Classes/**/*'
s.resource_bundles = {
'BOTestTools' => ['BOTestTools/Assets/*.xcassets']
}
end
复制代码
BOTestTools/Assets/*.xcassets
为图片资源的路径,须要和你存放图片的路径保持一致。
因为私有库中的Bundle内的资源没法直接访问,因此添加 BOTools
类来获取资源。 向 Classes 目录下添加 BOBundleTool.swift 文件。
import UIKit
public class BOTools {
static var bundle: Bundle = {
let bundle = Bundle.init(path: Bundle.init(for: BOTools.self).path(forResource: "BOTestTools", ofType: "bundle", inDirectory: nil)!)
return bundle!
}()
public static func getBundleImg(with name: String) -> UIImage? {
var image = UIImage(named: name, in: bundle, compatibleWith: nil)
if image == nil {
image = UIImage(named: name)
}
return image
}
}
复制代码
BOTools 类也须要添加 public 关键字,不然外界没法访问。
其中须要特别注意的是bundle路径的获取。
Bundle.init(for: BOTools.self).path(forResource: "BOTestTools", ofType: "bundle", inDirectory: nil)
复制代码
forResource参数:必须和组件库的名称保持一致。
复制代码
添加图片完成后,必定要执行 pod update
才能够在 Example 工程中访问。
在视图上添加 UIImageView,同时给按钮绑定点击事件。实现点击按钮,加载图片。
加载图片的代码以下:
let img = BOTools.getBundleImg(with: "1")
imgView.image = img
复制代码
运行结果以下:
ps:若是图片没法加载或者报错,能够 clean 一下项目,而后重试。
实现到这一步,基本上一个组件库已经制做完成了。可是如今还只是在本地,只能本身使用。若是和你一块儿协同开发的小伙伴也要使用你的库,那么你还须要将组件库上传至git。
在上传以前,还须要验证 podspec 是否正确。不然别人是没法使用的。
cd 到 BOTestTools 文件夹下。其目录下有 BOTestTools.podspec 文件。
在执行以下命令:
pod lib lint --allow-warnings
复制代码
执行结果以下:
若是出现:BOTestTools passed validation. 那么说明本地校验经过。
可是会发现有两个WARN警告⚠️,小伙伴可能也会遇到,那么咱们来看看这两个警告是什么意思。
summary: The summary is not meaningful.
这是由于你没有修改 .podspec 文件中的 s.summary
字段。
只须要修改 .podspec 文件便可。以下:
s.summary = '这是一个测试组件库'
复制代码
[iOS] swift: The validator used Swift 3.2 by default because no Swift version was specified.
这是由于咱们尚未指定当前组件库中Swift的使用版本。
在当前目录下执行以下命令,指定Swift版本为4.0:
echo "4.0" > .swift-version
复制代码
解决完上面两个WARN以后,再执行pod lib lint --allow-warnings
命令就会发现没有警告了。
将代码提交到git仓库: 一、执行 git add . 二、执行 git commit -m 'first commit',注意写好注释 三、执行 git push origin master 将代码提交到远程仓库
如今你能够在远程仓库中看到你提交的代码了。
仅将代码提交到git仓库还不够,还须要打上tag。而且该tag须要和 .podspec 文件中的版本 s.version = '0.1.0'
保持一致。
四、执行 git tag 0.1.0 打好tag 五、执行 git push --tags 将tag推送到git仓库
在上一步上将组件库提交到git仓库后,你的小伙伴仍是没法使用。还须要将 .podspec 文件提交到 BOTestSpec git库中。
执行以下命令:
pod repo push BOTestSpec BOTestTools.podspec --allow-warnings
复制代码
pod repo push 【私有库名称】 【podspec文件名】 --allow-warnings
复制代码
结果以下:
则说明,BOTestSpec库上传成功。
同时,你还能够查看 ~/.cocoapods/repos。在 BOTestSpec 仓库下会多新增 BOTestTools 文件夹。以下所示:
**至此,你的私有库已制做完成。**你和你的小伙伴们能够在项目中使用它了。
建立 【BOSpecDemo】 测试项目。
在项目目录下,执行命令:
pod init
复制代码
编辑 podfile 文件:
platform :ios, '9.0'
source 'https://github.com/CocoaPods/Specs.git' # 官方库
source 'https://github.com/Lwindy/BOTestSpec.git' # 私有库Repo地址
target 'BOSpecDemo' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for BOSpecDemo
pod 'BOTestTools'
end
复制代码
注意:私有仓库的地址必定是Spec Repo 的地址,不要错误的使用BOTestTools组件的git仓库。
而后执行 pod install
,添加私有库到工程中。
效果以下:
总结:私有库的制做不难,只是步骤比较繁琐。上述6步是我在实际使用中总结的步骤顺序,虽然不甚精简,但胜在稳定,基本能够一次性成功。
易错点: 一、混淆私有库repo仓库与组件仓库,之间的区别能够查看官方文档。 二、组件库制做完成后,要先push到git仓库再将.podspec push到repo仓库。 三、组件库必定要打tag标签。而且tag要和版本号一致。 四、.podspec 文件中的 source_files 和 resource_bundles 路径必定要正确。