开发一个iOS公共库并发布到CocoaPods的流程
可以参考这篇文章发布组件到CocoaPods
总的来说流程主要是:
- 创建一个组件仓库地址,比如GitHub仓库
- 创建本地组件代码库文件,利用的是
pod lib create <组件名>
,得到一个工程,这个工程其实就是一个完整的iOS项目,只不过它的Podfile文件中安装你写的公共组件是上层相对目录,而不是远程仓库,这样每次pod install
(在Example目录里)时相当于安装了本地组件代码库。与此同时,此Example文件夹中的这个工程就是用来测试你所开发的组件的,这样就可以一边开发一边调试。 - 开发调试完之后,修改.podspecs索引文件。
#
# Be sure to run `pod lib lint Dplayer.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'Dplayer'
s.version = '1.0.3'
s.summary = 'A video player.'
# 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
A video player developed by Swift.
DESC
s.homepage = 'https://github.com/weifengsmile/Dplayer'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'sidney' => '[email protected]' }
s.source = { :git => 'https://github.com/weifengsmile/Dplayer.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '10.0'
s.source_files = 'Dplayer/Classes/**/*'
# s.resources = ['Dplayer/Assets/*.{xcassets}', 'Dplayer/Assets/*.gif']
s.resource_bundles = {
'Dplayer' => ['Dplayer/**/*.{xib,xcassets,gif,png,jpg,jpeg}'],
}
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
s.dependency 'SnapKit', '~> 5.0.0'
s.dependency 'Toast-Swift', '~> 5.0.1'
s.swift_version= "4.2"
end
- 提交所有改动到git远程仓库中,并打tag,tag号一定要与podspec中的版本号一致。
- 验证podspec:
pod spec lint --verbose --allow-warnings
, 如果出错,按提示处理,成功会提示xxxpodspec passed validation.
- 发布:
pod trunk push xxxx.podspec
(若未注册trunk,需要先注册)
有哪些问题?
Could not load NIB in bundle
nib无法加载,比如- 自定义UIView对应的xib文件、图标、图片等资源文件,需要将它放入podspec中的
s.resource_bundles
或者s.resources
并且一定要指明资源*路径。 【强烈建议放在resource_bundles中,可以避免资源重名等问题】 - 无论放入哪个里,都应该注意,不能用Bundle.main去获取资源,因为当前已经不是主工程的bundle而是公共库的bundle。但是二者获取bundle的方式稍有不同:
// 放入s.resources中,获取bundle的方式
public func getBundle() -> Bundle {
// DplayerView为该bundle中任意一个类
return Bundle.init(for: DplayerView.classForCoder())
}
// 放入s.resource_bundles中,获取bundle的方式
public func getBundle() -> Bundle {
let bundlePath = URL(fileURLWithPath: Bundle(for: DplayerView.self).resourcePath ?? "").appendingPathComponent("/Dplayer.bundle").path
let resource_bundle = Bundle(path: bundlePath) ?? Bundle(for: DplayerView.self)
return resource_bundle
}
// 获取xib
let viewFromXib = getBundle().loadNibNamed("DplayerView", owner: self, options: nil)![0] as! UIView
viewFromXib.frame = self.bounds
addSubview(viewFromXib)
// 获取UIImage
public class func getUIImageByName(_ name: String) -> UIImage? {
let result = UIImage(named: name, in: getBundle(), compatibleWith: nil)
return result ?? nil
}