创建远程私有管理库
-
在远程服务器(例:码云、Coding等)创建私有管理库
- 码云:https://gitee.com
- Coding: https://coding.net
就是新建一个私有项目
-
本地添加私有管理库
-
$ pod repo add HQSpecs git@gitee.com:H-Joe/HQSpecs.git
7.png
-
创建私有库(组件)
-
创建私有库(组件)
-
创建模版库:
$ pod lib create HQBase
使用模版库的好处是,会自动生成测试工程,可以边开发库边测试。
如果创建不成功,可能原因之一是
Xcode
和cocoapods
版本不匹配,升级cocoapods
:$ sudo gem install cocoapods
-
设置配置信息
8.png
9.png -
生成的模版如下:
10.png -
把要做成私有库(组件)的代码放入
Classes
中,把其他资源文件(图片、文件等)放在Assets
中
11.png -
在模版中的测试工程
Example
中,已经配置好了Podfile
,只需pod install
即可
12.png
- 如果私有库(组件)中有依赖系统库,则需配置
s.frameworks
或s.library
- 如果私有库(组件)中有依赖第三方库,类似
SDWebImage
、AFNetworking
等,需要在HQBase.podspec
中添加依赖关系
13.png
- 本地测试
-
-
推送到远程服务器
-
在远程服务器上创建私有库(组件)项目
14.png -
配置
HQBase.podspec
文件homepage
是私有库(组件)仓库的地址,source
是私有库(组件)代码地址15.png -
本地验证
HQBase.podspec
配置是否正确$ pod lib lint
- 或
$ pod lib lint --allow-warnings
16.png -
提交远程仓库
-
git
管理$ git add .
$ git commit -m "描述信息"
$ git remote add 远程代码地址
$ git push origin master
-
增加标签
$ git tag '0.1.0'
此标签号必须与
HQBase.podspec
中的s.version
一致$ git push --tags
-
远程验证:
$ pod spec lint --allow-warnings
17.png
-
-
-
推送到远程私有管理库
$ pod repo push 远程私有管理库 本地个人私有库.podspec
-
例:
$ pod repo push HQSpecs HQBase.podspec
18.png
-
使用
可以通过
pod search
来搜索查询私有组件-
$ pod search HQBase
19.png20.png -
在宿主项目,直接在
Podfile
文件中配置
21.png
需要注意的是,必须配置两个源,这样就可以同时使用公有库和私有库
升级私有库(组件)
-
添加代码到
Classes
中
22.png -
本地测试
- 在
Example
中$ pod install
- 编译测试通过
- 在
-
推送到远程服务器
-
配置
HQBase.podspec
文件,version
版本增加
23.png -
git
管理$ git add .
$ git commit -m "描述信息"
$ git push origin master
-
增加标签
$ git tag '0.2.0'
$ git push --tags
远程验证:
$ pod spec lint --allow-warnings
-
-
推送到远程私有管理库
$ pod repo push HQSpecs HQBase.podspec
-
使用
- 宿主项目中,
$ pod update
即可。
- 宿主项目中,
子私有库(组件)
如果私有库(组件)有很多功能,我们所用的只是其中的一个子功能,就有必要创建子私有库(组件)
- 配置
HQBase.podspec
文件,添加子私有库(组件)增加版本号
version
-
设置依赖
dependency
依赖放在具体的子索引库中,谁需要,谁设置,不需要,不设置
-
增加
subspec
24.png 本地验证:
$ pod lib lint --allow-warnings
- 推送到远程服务器
-
git
管理$ git add .
$ git commit -m "描述信息"
$ git push origin master
- 增加标签
$ git tag '0.3.0'
$ git push --tags
- 远程验证:
$ pod spec lint --allow-warnings
-
- 推送到远程私有管理库
$ pod repo push HQSpecs HQBase.podspec
- 使用
-
$ pod search HQBase
26.png -
宿主项目中,可以
$ pod install
需要的子功能
25.png
-
私有库(组件)中资源文件的使用
-
xib
-
在私有库(组件)中加载
xib
文件需注意以下几点:-
- 所有使用
bundle
的地方都必须动态获取
[NSBundle bundleForClass:[self class]]
27.png - 所有使用
-
- 不能使用
mainBundle
, 因为此时的xib
文件不在mainBundle
下,是在xxx.framework
下
28.png - 不能使用
-
-
使用
UILabel *l = [[UILabel alloc]init]; l.frame = CGRectMake(30, 20, 300, 30); l.text = @"1、测试加载xib文件"; [self.view addSubview:l]; TestXibView *t = [[NSBundle bundleForClass:[self class]] loadNibNamed:@"TestXibView" owner:nil options:nil].firstObject; t.frame = CGRectMake(30, 60, 300, 100); [self.view addSubview:t];
-
- 图片
-
图片资源放置的位置是
Assets
30.png -
配置
HQMain.podspec
文件
31.png -
在私有库(组件)中,图片资源的存放位置和
xib
文件一样,已经不在mainBundle
下,是在xxx.framework
下的xxx.bundle
中32.png -
使用
-
-
xib
中使用
- 在图片名称前面添加组件主
bundle
, 本例中就是HQMain.bundle
33.pngUILabel * l = [[UILabel alloc]init]; l.frame = CGRectMake(30, 180, 300, 30); l.text = @"2、测试加载xib上有图片"; [self.view addSubview:l]; TestXibImageView * t = [[NSBundle bundleForClass:[self class]] loadNibNamed:@"TestXibImageView" owner:nil options:nil].firstObject; t.frame = CGRectMake(100, 220, 120, 100); [self.view addSubview:t];
-
-
- 代码加载
- 不能使用
imageNamed
方法 - 使用
imageWithContentsOfFile
方法 -
核心代码
35.png
UILabel * l = [[UILabel alloc]init]; l.frame = CGRectMake(30, 340, 300, 30); l.text = @"3、测试加载图片"; [self.view addSubview:l]; NSBundle *currentBundle = [NSBundle bundleForClass:[self class]]; NSString *bundleName = [currentBundle.infoDictionary[@"CFBundleName"] stringByAppendingString:@".bundle"]; NSString *path = [currentBundle pathForResource:@"aixin.png" ofType:nil inDirectory:bundleName]; UIImage *image = [UIImage imageWithContentsOfFile:path]; UIImageView * i = [[UIImageView alloc]init]; i.image = image; i.frame = CGRectMake(30, 380, 300, 250); [self.view addSubview:i];
-
在加载图片时,图片名称必须是全名, 如:
aixin.png
、aixin@2x.png
、aixin@3x.png