前言
最近一直在尝试组件化一些常用模块,且希望将代码托管在远程平台(github/码云/SVN)上以CocoaPods管理其版本迭代,这对开发无疑是非常便利的,也可更快的提高开发效率!
在开发过程中查阅了很多相关资料,也踩过不少雷,这里将自己的心得记录下来分享一下,也权当笔记整理思路了。
下面直接上正文,CocoaPods的安装与使用,这里就不再赘述了,网上资源很多。
创建工程
在终端用命令行创建工程,生成"SSDemo"工程:,如下:
pod lib create SSDemo
创建远程仓库托管(github/码云)
1.开源库创建:建议使用github托管,毕竟在github上搜索开源库的人员多很多,当然如果不想被被人搜索到并使用,那就无所谓了;
2.私有库创建:建议使用码云(gitee)托管,github私有仓库是需要收费的,码云免费,so…你懂得!
话不多说,以在码云(gitee)上创建私有库为例,直接上图:
3.将自己刚才创建的工程"SSDemo"传到码云上,或用命令行或用SourceTree上传,不啰嗦都懂;
编辑podspec文件
1.编辑.podspec文件,这个文件是一个描述pod库版本文件,这是官网说明地址:http://guides.cocoapods.org/syntax/podspec.html
Pod::Spec.new do |s|
s.name = 'SSDemo' #存储库名称
s.version = '0.1.0' #版本号,与tag值一致
s.summary = 'A short description of SSDemo.' #简介
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC #描述
s.homepage = 'https://gitee.com/xxx/SSDemo' #项目主页,这里默认创建的可能是'https://github.com/xxx/SSDemo',没关系换掉自己的就可
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' } #开源协议
s.author = { 'ss' => 'ss' } #作者
s.source = { :git => 'https://gitee.com/xxx/SSDemo.git', :tag => s.version.to_s } #存储库的git地址,以及tag值(删掉默认,替换自己的)
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0' #支持的平台和版本号
s.requires_arc = true #是否支持ARC
s.static_framework = true #是否使用静态库。如果podfile指明了 use_frameworks!命令,但是pod仓库需要使用静态库则需要设置,否则报错
s.source_files = 'SSDemo/Classes/**/*' #需要托管的源代码路径
# s.resource_bundles = { #资源文件,没有就不用写
# 'SSDemo' => ['SSDemo/Assets/**/*']
# }
s.vendored_frameworks = ['SSDemo/Classes/xxx.framework'] #私有库文件(eg:自己封装的库文件),没有就不用写
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# 所依赖的第三方库,没有就不用写,这里写几个作为例子
s.dependency 'Masonry' #布局
引入需要托管的源码/文件
导入需要托管的代码与文件,plist文件/bundle文件等都可放到Assets文件下,如下图:
测试工程
1.切换到Example文件目录下面使用命令
pod install
2.编译文件,并测试调用托管文件
#import "SSViewController.h"
#import <SSDemo/SSPersonModel.h>
@interface SSViewController ()
@end
@implementation SSViewController
- (void)viewDidLoad{
[super viewDidLoad];
SSPersonModel *pModel = [SSPersonModel new];
pModel.name = @"Tim";
pModel.age = @"20";
NSString *des = [pModel getPersonDes];
NSLog(@"%@",des);
}
@end
结果
2019-08-28 11:27:07.205844+0800 SSDemo_Example[2829:1105517] 姓名:Tim,年龄:20
校验
代码/文件配置完成后,提交SVN/Git服务器之前,最好先让CocoaPods做一下校验,校验通过后再提交,以确保你的库能够被其他项目正常导入。
1.切换到SSDemo文件目录下面使用命令:
pod lib lint SSDemo.podspec //校验podspec文件
xxx-Pro:SSDemo xxx$ pod lib lint SSDemo.podspec
-> SSDemo (0.1.0)
- WARN | summary: The summary is not meaningful.
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
[!] SSDemo did not pass validation, due to 2 warnings (but you can use `--allow-warnings` to ignore them).
You can use the `--no-clean` option to inspect any issue.
出现警告, 因为未修改简介和描述,可以用 --allow-warnings来忽略警告
xxx-Pro:SSDemo yj_zhang$ pod lib lint SSDemo.podspec --allow-warnings
-> SSDemo (0.1.0)
- WARN | summary: The summary is not meaningful.
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
SSDemo passed validation.
//表明验证通过
切换到SSDemo.podspec文件所在的目录下,终端输入命令
2.设置tag
git tag 0.1.0 //tag值与.podspec中的version一致
git push origin 0.1.0
3.验证
pod spec lint SSDemo.podspec
出现警告,同上用 --allow-warnings来忽略警告
xxx-Pro:SSDemo xxx$ pod spec lint SSDemo.podspec --allow-warnings
-> SSDemo (0.1.0)
- WARN | summary: The summary is not meaningful.
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
Analyzed 1 podspec.
SSDemo.podspec passed validation.
//验证通过
提交代码到托管平台
pod trunk push SSDemo.podspec --allow-warnings //这里直接添加忽略警告
Updating spec repo `master`
CocoaPods 1.8.0.beta.2 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.8.0.beta.2
Validating podspec
-> SSDemo (0.1.0)
- WARN | summary: The summary is not meaningful.
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
Updating spec repo `master`
CocoaPods 1.8.0.beta.2 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.8.0.beta.2
-------------------------------------------------------------------------------
? Congrats
? SSDemo (0.1.0) successfully published
? August 28th, 00:08
? https://cocoapods.org/pods/SSDemo
? Tell your friends!
-------------------------------------------------------------------------------
至此上传完毕
引用
1.验证搜索一下
开源库搜索 : pod search SSDemo
私有库搜索 : pod search SSDemo,自己可搜索,他人搜不到
pod search SSDemo
2.项目中引用
私有库的引用方式与开源库的引用方式并不相同
1.开源库
pod 'SSDemo' , '~> 1.0'
2.私有库
pod 'SSDemo', :tag => '0.1.0', :git => 'https://gitee.com/xxx/xx.git' //后面跟存储库的git地址
报错
1.出现这个问题,说明Podfile文件中使用了’use_frameworks!’,而podspec文件中没有设置’s.static_framework = true’ #是否使用静态库。
如果podfile指明了use_frameworks!命令,但是pod仓库需要使用静态库则需要设置,否则报错!
[!] The 'Pods-SSDemo_Example' target has transitive dependencies that include static binaries: (/Users/xxx/xxx/SSDemo/Example/Pods/UMCCommon/UMCommon.framework, /Users/xxx/xxx/SSDemo/Example/Pods/UMCSecurityPlugins/thirdparties/SecurityEnvSDK.framework,…)
2.这个错误是因为依赖库(s.dependency)包含了.a静态库造成的。虽然这并不影响Pod的使用,但是验证是无法通过的,可通过 --use-libraries 来让验证通过,同时包含警告,添加 --allow-warnings。
ERROR | [iOS] unknown: Encountered an unknown error (The 'Pods-App' target has transitive dependencies that include static binaries: (...))during validation.
解决办法
//--verbose 来显示检查编译的详细信息
pod spec lint SSDemo.podspec --verbose --use-libraries --allow-warnings
3.这主要是因为在本地索引里面没有
[!] Unable to find a pod with name, author, summary, or description matching `SSDemo`
解决办法,删除search_index.json,重新搜索
rm ~/Library/Caches/CocoaPods/search_index.json
pod search SSDemo