(一)、自做的远程索引库
source 'https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git'
source 'https://github.com/CocoaPods/Specs.git'
索引库
https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git
视频播放库 (完成)
版本:1.0.0
pod 'ZZMakeAVPlayer'
https://github.com/UserZhangXiaoZhe/ZZMakeAVPlayer.git
FMDB数据库工具(完成)
版本:0.0.1
pod 'ZZMakeFMDBManager'
https://github.com/UserZhangXiaoZhe/ZZMakeFMDBManager.git
iOS WIFI电脑给手机传文件 (完成)
版本:1.0.0
pod 'ZZMakeWiFiManager'
https://github.com/UserZhangXiaoZhe/ZZMakeWiFiManager.git
自定义类别(完成)
版本:1.0.1
pod 'ZZMakeCategory'
https://github.com/UserZhangXiaoZhe/ZZMakeCategory.git
(二)、详细步骤:
1.github上创建ID,添加一个公开的索引库ZZMakeSpecs.
2.终端上查看本地索引库:
pod repo, 目前本地仅有github上的那个公有索引库.
3.终端添加私有索引库:
// pod repo add 索引库名称 索引库地址
pod repo add ZZMakeSpecs https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git
现在有2个本地索引库。
4.github创建组件库 ZZMakeAVPlayer
5.到合适的位置创建一个与组件名相同的文件夹,
cd进去后,使用如下命令
// pod lib create 组件名
pod lib create ZZMakeAVPlayer
按需回答一些问题
6.设置git凭证:
Name:###
E-mail:###
7.前往你创建的文件夹
把基础组件相关的东西丢到Classes文件夹中,并且把ReplaceMe.m文件删除
默认Classes文件夹中存放的文件就是pod install时要下载下来的文件,当然可以通过修改spec文件的配置来更改位置.
8.修改Spec:
s.name = 'ZZMakeAVPlayer'
s.version = ‘1.0.0’ 每次更新更改
s.summary = 'ZZMakeAVPlayer.'
s.description = <<-DESC
ZZMakeAVPlayer一个播放视频的工具
DESC
s.homepage = 'https://github.com/UserZhangXiaoZhe/ZZMakeAVPlayer'
s.source = { :git => 'https://github.com/UserZhangXiaoZhe/ZZMakeAVPlayer.git', :tag => s.version.to_s }
s.source_files = 'ZZMakeAVPlayer/Classes/**/*.{h,m}'
s.ios.deployment_target = '10.0'
s.dependency 'AFNetworking', '~> 2.3'
修改完要cd 到Example文件下,pod install
9.cd 到Example文件下,
pod install ,关闭程序重新打开,编译运行确保组件的可用。
依赖会自动添加进去
10.将代码提交到组件仓库. 每次更新从这就行
//cd 到ZZMakeAVPlayer文件下
git init
git add .
git commit -m ‘第一次添加组件’
git remote add origin https://github.com/UserZhangXiaoZhe/ZZMakeAVPlayer.git
git push origin master
输入用户密码
(注意:"Please tell me who you are"问题
进入项目根目录,里面有一个.git的隐藏文件,使用 ls -a 命令可以看到,
cd .git进文件,有一个config文件,vim config 编辑文件.
编辑config文件,在最后添加如下内容:
[user]
name = ###
email = ###
敲esc键-->打出:冒号-->分别敲w q-->敲回车 ,保存退出
)
11.打标签,标签0.1.0与spec中的s.version保持一致,提交一次数字增加一次
git tag ‘1.0.0’
git push --tags
12.提交podspec到私有索引库,先做一个验证
本地验证Spec的必填字段
// 本地验证不会验证 s.source 中的tag , cd 到ZZMakeAVPlayer.podspec路径下
pod lib lint ZZMakeAVPlayer.podspec --allow-warnings
远程验证
// 远程验证会验证 s.source 中的tag,如果此时没有打上相应的标签则会报错
pod spec lint --allow-warnings
验证私有库提示,如果验证的是私有库,则在后面加上--private,否则会有警告,你可以选择--allow-warnings来忽略该警告
pod lib lint --private
pod spec lint --private
13.验证成功提交podspec文件到索引库,会有各种报错
// pod repo push 私有索引库名称 spec名称.podspec
//pod repo push ZZMakeSpecs ZZMakeAVPlayer.podspec
//最好加上私有库地址和官方库地址
pod repo push --sources=https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git,https://github.com/CocoaPods/Specs.git ZZMakeSpecs ZZMakeFMDBManager.podspec
--verbose 添加后面可查看详细
操作过程:先将我们的代码直接push到本地索引库ZZMakeSpecs,推送后会自动帮我们同步到远程索引库。
1.报错:[!] The repo `MySpecs` at `../../../.cocoapods/repos/ZZPublicSpecs` is not clean
解决方法:
1》移除本地私有索引库:pod repo remove ZZPublicSpecs
2》添加本地私有索引库:pod repo add ZZMakeSpecs https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git
3》添加本地podspec文件到远程私有索引库即可:pod repo push ZZMakeSpecs ZZMakeAVPlayer.podspec --allow-warnings
2.报错:Your configuration specifies to merge with the ref 'refs/heads/master' from the remote, but no such ref was fetched.
创建远端索引库的时候没有创建readme.md文件,也就是是个空仓库。在提交的时候一直报错卡在更新索引库这步。网上找了半天也没找到原因,最后重新生成了一个仓库,添加了readme.md文件。在去提交索引库就可以了。先前那个提交一直报错的那个,我复制了个readme.md文件提交上去,然后再去提交索引库就可以了。所以创建索引库的时候一定不能是个空仓库里面有啥都行就是不能为空。
解决办法:随便往仓库放写东西。
3.报错:Failed to open TCP connection to raw.githubusercontent.com:443
在safari浏览器里访问以下网址,再执行就没问题了。
4.报错:failed to push some refs to
通过以下命令进行代码合并
git pull --rebase origin master
这时候就可以在本地文件内看到README.md文件
之后再运行git push origin master 进行推送
5.报错:[!] The `xxx.podspec` specification does not validate. 但是没有ERROR
执行:pod repo push --sources=https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git,https://github.com/CocoaPods/Specs.git ZZMakeSpecs ZZMakeFMDBManager.podspec --use-libraries --allow-warnings
6.报错[!] Unable to add a source with url `` named `-1`.
解决:给索引库都添加上路径,带上官方的路径
pod repo push --sources=https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git,https://github.com/CocoaPods/Specs.git ZZMakeSpecs ZZMakeFMDBManager.podspec
14.测试下搜索我们的组件
pod search 'ZZMakeAVPlayer'
如果找不到,请输入如下命令:
pod setup
命令执行完毕后,如果还是搜索不到,请删除spec本地索引库,重试一遍
// 删除索引文件:
rm ~/Library/Caches/CocoaPods/search_index.json
// 删除成功后再次搜索:
pod search
15。添加Podfile文件
pod init
vim Podfile 进入文件,点击i开始编辑
在Podfile的最顶部添加如下描述
// 第二行是为了保证公有库的正常使用
source 'https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git'
source 'https://github.com/CocoaPods/Specs.git'
添加使用组件ZZMakeAVPlayer
pod 'ZZMakeAVPlayer'
敲esc键-->打出:冒号-->分别敲w q-->敲回车 ,保存退出
安装
pod install
(三)、其他
删除索引库
pod repo remove master
问题 1. 维护阶段(扩充代码,版本号升级)
需重新提交代码 修改spec描述文件,大概步骤如下:
1. cd 到.spec文件下,重新提交代码
git add .
git commit -m ‘更改’
git push origin master
2. 改spec描述文件,打tag
git tag 1.0.1
git push --tags
3. 提交pec文件
4. 更新spec文件到索引库
pod repo push ZZMakeSpecs 文件名称.podspec
问题 2. 依赖关系解决(比如制作库需要依赖AFN)
//直接spec文件描述即可
s.dependency 'AFNetworking', '~> 3.1.0'
问题 3. 依赖关系子库解决
(如一个库里有三个子库,现在只需用到其中一个,不要全部用到 ,即把一个库分成几个小库)
# 代表分离子库
#s.source_files = 'xx/Classes/**/*' 这种写法是匹配所有的
s.subspec 'Category' do |c|
c.source_files = 'xx/Classes/Category/**/*'
end
s.subspec 'Network' do |n|
n.source_files = 'xx/Classes/Network/**/*'
n.dependency 'AFNetworking', '~> 3.1.0'
end
s.subspec 'Tool' do |t|
t.source_files = 'xx/Classes/Tool/**/*'
end
然后使用这个子库过程podefile 写法
target 'Example' do
use_frameworks!
pod 'xx/Category'
pod 'xx/Network'
end
Search pod搜索不到
1.先移除掉本地的master,在终端输入pod repo remove master
2.然后转到路径下,在终端输入cd ~/.cocoapods/repos
3.再把github上的spec下载下来,终端输入命令:git clone --depth 1 https://github.com/CocoaPods/Specs.git master
4.再执行pod install --no-repo-update就能够正确更新这些第三方库了.
(四)、给自己的组件添加资源文件
1.如何把资源文件打包为.bundle文件?
通常我们都会用.bundle文件把资源文件打包,这里也一样,把你的图片、音频、NIB等资源文件先统一放在一个文件夹里,文件夹名称最好是你的组件名称加上bundle,然后修改文件夹后缀为.bundle,就得到了一个bundle文件;然后把.bundle文件放在与Classes同一级的文件夹下.
Pod会自动添加到组件里
2.如何在podspec文件里配置资源路径?
然后在podspec文件里通过s.resource指定你的资源文件路径,如果你的组件名称叫ZZMakePlayer,那么你的bundle名称就可以叫ZZMakePlayerBundle.bundle
在podspec文件里配置:
s.resource = 'ZZMakePlayer/ZZMakePlayerBundle.bundle'
3.如何读取bundle里面的资源?
首先我们需要获取bundle,你是不是立即想到了NSBundle mainBundle ? 但是当你的 pod 库以 framework 形式被使用时,你的资源不是被拷贝到 mainBundle 下,而是被放到 pod 的最终产物—— framework里。此时,你必须保证自己在访问这个 framework 的 bundle,而不是主工程的。
创建了一个NSBundle的类别文件
先获取framework 的 bundle,ZZAVPlayerView.h是组件里的某个类文件:
@interface NSBundle (ZZAVPlayerBundle)
+ (instancetype)getCurrentBundle;
@end
#import "ZZAVPlayerView.h"
@implementation NSBundle (ZZAVPlayerBundle)
+ (NSURL *)zzAVPlayerResourcesBundleURL {
//先获取framework 的 bundle
NSBundle *bundle = [NSBundle bundleForClass:[ZZAVPlayerView class]];
return [bundle URLForResource:@"ZZMakePlayerResources" withExtension:@"bundle"];
}
+ (instancetype)getCurrentBundle {
//再获取我们自己手动创建的bundle
return [self bundleWithURL:[self zzAVPlayerResourcesBundleURL]];
}
@end
获取到了我们存放资源文件的bundle文件以后,就可以访问资源文件了,
创建一个类别:
@interface UIImage (ZZAVPlayerImage)
+ (UIImage *)getImageWithNamed:(NSString *)imageName;
@end
#import "NSBundle+ZZAVPlayerBundle.h"
@implementation UIImage (ZZAVPlayerImage)
+ (UIImage *)getImageWithNamed:(NSString *)imageName{
NSString *realName = [NSString stringWithFormat:@"%@@2x",imageName];
NSString *path = [[NSBundle getCurrentBundle] pathForResource:realName ofType:@"png"];
UIImage *loadingImage = [[UIImage imageWithContentsOfFile:path] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
return loadingImage;
}
@end