小知识,大挑战!本文正在参与“程序员必备小知识”创作活动
通过问题看本质!!!
CocoaPod库制作完成后,在使用过程中往往会有一些其他操作,比如引入资源文件和创建分支。
1.引用图片、xib、storyboard、plist等资源文件
将资源拖到pod的CTLFoundation的Assert目录下,可以分文件,在podspec中配置如下:
s.resources = ['CTLFoundation/Assets/*/*.png','CTLFoundation/Classes/**/*.xib']
或
s.resource_bundles = {
'CTLFoundation' => ['CTLFoundation/Assets/*/*.png','CTLFoundation/Classes/**/*.xib']
}
利用 resources 属性时,这些资源文件build时会被直接拷贝到client target的mainBundle里。但是,这就带来了一个问题,那就是 client target 的资源和各种 pod 所带来的资源都在同一 bundle 的同一层目录下,很容易产生命名冲突。为了解决这一问题,CocoaPods 在 0.23.0 加入了一个新属性 resource_bundles。当然前者也有解决方案。资源的访问,self为pod中的任意类: 前者:
+ (UIImage *)imageInBoundleWithName:(NSString *)name {
NSBundle *boundle = [NSBundle bundleForClass:[self class]];
UIImage *image = [UIImage imageNamed:name inBundle:boundle compatibleWithTraitCollection:nil];
return image;
}
后者:
+ (UIImage *)imageInBoundleWithName:(NSString *)name {
NSBundle *boundle = [NSBundle bundleForClass:[self class]];
NSBundle *currentBoundle = [NSBundle bundleWithPath:[boundle pathForResource:@"CTLFoundation" ofType:@"bundle"]];
UIImage *image = [UIImage imageNamed:name inBundle:currentBoundle compatibleWithTraitCollection:nil];
return image;
}
加载xib: 前者:
NSBundle *xibBundle = [NSBundle bundleForClass:[self class]];
CTLView *view = [xibBundle loadNibNamed:@"CTLView" owner:nil options:nil].lastObject;
后者:
NSBundle *xibBundle = [NSBundle bundleForClass:[self class]];
NSBundle *currentBundle = [NSBundle bundleWithPath:[xibBundle pathForResource:@"CTLFoundation" ofType:@"bundle"]];
CTLView *view = [currentBundle loadNibNamed:@"CTLView" owner:nil options:nil].lastObject;
2.创建分支pod库
创建分支pod库流程与上述流程不一样,具体如下:
- 像以上pod库制作一样,通过
-
$ pod lib create BranchRepoDemo - 创建pod工程文件,并按照要求编辑podspec文件,podspec文件中的source要如下设置:
-
...... s.source = { :git => 'https://github.com/xxx/BranchRepoDemo.git', :branch => "dev" } ...... - 同样要保证通过
$ pod lib lint- 的校验, 不需要对工程打tag。
- 关联远程origin/master分支
- 在远程项目仓库中创建分支dev
- 在本地项目仓库中有以下操作:
终端中Git分支相关命令
$ git checkout -b dev //新建并切换到本地dev分支
$ git pull origin dev //本地分支与远程分支相关联
$ git push origin dev //本地分支推送到远程分支
成功推送后,即可使用,使用方法如下:
pod 'pod库名', :git => 'https://github.com/xxx/BranchRepoDemo.git', :branch => 'dev'