MainMoudle
和一个用于存放全部本地私有库的文件夹AllMoudles
, 这两个文件夹在同一目录下AllMoudles
文件夹中建立一个私有库TitanFMBase
, 在子目录建立Classes
用于存放全部的文件, 目录以下: AllMoudles/TitanFMBase/Classes
Classes
文件夹中添加文件, 并提交到本地git
//进入TitanFMBase文件夹
cd xxx/AllMoudles/TitanFMBase
//初始化git
git init
//将本地代码提交到本地仓库
git add .
// 提交修改到本地仓库
git commit -m '你的修改记录'
//建立spec文件
pod spec cteate TitanFMBase
复制代码
最后打开TitanFMBase
文件夹中的TitanFMBase.podspec
, 修改对应的配置信息, 可参考修改博客html
注意点ios
在source
配置中, 本地库的git地址不须要填git
s.source = { :git => "", :tag => "#{s.version}" }
复制代码
path
的形式添加框架依赖pod 'TitanFMBase', :path => '../AllMoudles/TitanFMBase'
复制代码
注意点github
pod lib lint
或者pod spec lint
验证spec
文件的正确性pod search
命令时, 搜索的实际上是本地缓存的spec
文件, 固然第一次使用时须要先更新本地的spec
文件pod repo
命令查看当前本地的索引库, 或者查看目录~/.cocoapods/repos/master/Specs
podspec
文件pod repo add TitanSpec http://xxxx
命令, 可建立一个新的本地索引库pod lib create xxx
spec
文件和测试工程等xxx/xxx/Classes
文件夹下便可, 默认建立的.m文件可删除Example
目录下的测试工程, 并执行pod install
命令, 将你的私有库文件安装到测试工程xxx
文件下的xxx.podspec
文件中相关配置便可作完上述工做便可将项目全部文件提交到远程私有库了缓存
// 将本地代码加入本地仓库里
git add .
// 提交修改到本地仓库
git commit -m '你的修改记录'
// 查看当前的远程链接
git remote
// 添加名称为origin的远程链接
git remote add origin '你的github项目地址'
// 在push以前, 查看spec是否配置有问题
// 验证本地spec文件是否有误
pod lib lint
// 验证远程spec文件是否有误
pod spec lint
// 推送master分支的代码到名称为origin的远程仓库
git push origin master
复制代码
s.source
后面的tag
tag
, 而至此咱们的tag
尚未设置, 因此验证不会经过, 须要打标签tag
, 再次验证应该就是没问题的了// 查看当前的tag值
git tag
// 设置tag值
git tag "0.0.1"
// 上传提交tag
git push --tags
// 删除标签相关命令
// 先删除本地再删除远程标签, 删除后须要从新打标签
// 删除本地标签
git tag -d 0.0.1
// 删除远程标签
git push origin :0.0.1
复制代码
向私有的SpecRepo
中提交podspec
:bash
pod repo push SpecName XXX.podspec
复制代码
注意点微信
podspec
的过程当中会有验证, 最好在提交以前先验证spec
文件的配置是有问题pod lib lint
--allow-warings
忽略pod search XXX
Podfile
文件中, 同事使用私有库和第三方库是须要指定对应的source
源pod repo
命令执行后的结果master
- Type: git (master)
- URL: 'https://github.com/CocoaPods/Specs.git'
- Path: /Users/xxx/.cocoapods/repos/master
TitanFMSpec
- Type: git (master)
- URL: 'https://git.coding.net/CoderTitan/TitanFMSpec.git'
- Path: /Users/xxx/.cocoapods/repos/TitanFMSpec
复制代码
Podfile
文件中配置信息app
// 远程私有库
source 'https://git.coding.net/CoderTitan/TitanFMSpec.git'
// 官方仓库
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
target 'TitanjunFM' do
use_frameworks!
pod 'TitanFMBase'
pod 'MJExtension'
end
复制代码
xxx/xxx/Classes
文件夹下对应的库文件Pod
库文件: pod update --no-repo-update
xxx.podspec
文件的配置信息, 版本号必定要改git push origin master
tag
标签: git push --tags
pod repo push SpecName XXX.podspec
podspec
文件配置中, 添加以下依赖代码s.dependency 'AFNetworking'
s.dependency 'SDWebImage'
复制代码
Podfile
文件中, 不会再导入该类库上述方案存在的问题: 假如另一个业务线, 仅仅须要依赖一些基础配置, 可是, 若是把整个库做为依赖, 便会导入一些不用的冗余代码框架
pod search AFNetworking
, 看一下AFNetworking
的搜索结果Subspecs
中, 将AFNetworking
分红了几个不一样的部分, 这样咱们就能够根据不一样的功能需求导入不一样部分的代码便可, 防止代码冗余-> AFNetworking (3.2.1)
A delightful iOS and OS X networking framework.
pod 'AFNetworking', '~> 3.2.1'
- Homepage: https://github.com/AFNetworking/AFNetworking
- Source: https://github.com/AFNetworking/AFNetworking.git
- Versions: 3.2.1, ......,0.5.1 [master repo]
- Subspecs:
- AFNetworking/Serialization (3.2.1)
- AFNetworking/Security (3.2.1)
- AFNetworking/Reachability (3.2.1)
- AFNetworking/NSURLSession (3.2.1)
- AFNetworking/UIKit (3.2.1)
复制代码
为解决将私有库中的代码分红不一样的功能模块, 使用
subspec
语法配置podspec
文件, 以下:组件化
//格式:
s.subspec 'XXX' do |x|
//须要导入的全部文件的相对路径
x.source_files = '相对路径/**/*'
//须要导入的.h头文件的相对路径
x.public_header_files = '相对路径/**/*.h'
//须要导入的资源文件的相对路径
x.resource = "相对路径/**/*.{bundle,nib,xib}"
//所依赖的其余的库
x.dependency 'AFNetworking', '~> 1.0.0'
end
//示例:
s.subspec 'Network' do |n|
n.source_files = 'TitanFMBase/Classes/Network/**/*'
n.dependency 'AFNetworking'
end
复制代码
s.source_files
改为上述语法便可pod 'AFNetworking/Reachability'
便可xib&storyboard
xib
必须动态获取Xib
资源时, 又该如何引用呢?Xib
时, 一般方式是[[NSBundle mainBundle] load]
方式, 可是这种方式在私有库中显然不适用XIb
, 使用方法[NSBundle bundleForClass:self]
动态获取, 具体看一下// MiddleView.m
NSBundle *mainBundle = [NSBundle mainBundle];
NSBundle *bundle = [NSBundle bundleForClass:self];
MiddleView *middleView = [[bundle loadNibNamed:@"MiddleView" owner:nil options:nil] firstObject];
// 打印一下上述两个bundle以下:
// mainBundle:
NSBundle </Users/xxx/Library/Developer/CoreSimulator/Devices/6B74958F-560F-4BF4-9BDF-9AD789379FC9/data/Containers/Bundle/Application/FC9747F0-8A82-4643-AC7E-BDC268190B8D/TitanFM.app>
// bundle:
NSBundle </Users/xxx/Library/Developer/CoreSimulator/Devices/6B74958F-560F-4BF4-9BDF-9AD789379FC9/data/Containers/Bundle/Application/FC9747F0-8A82-4643-AC7E-BDC268190B8D/TitanFM.app/Frameworks/TitanFMMain.framework>
复制代码
Xib
等资源文件是放在TitanFM.app
中的Xib
等资源文件是放在TitanFM.app/Frameworks/TitanFMMain.framework
文件目录下的, 因此私有库中的资源文件加载, 要到对应的文件目录下TitanFM.app
中查看, 找到对应的app
文件, 显示包内容, 便可层级查看.xcassets
的文件中Classes
的同级目录中会默认建立一个Assets
的文件夹, 用于存放图片等资源podspec
文件中, 一样修改加载文件资源的配置, 以下:s.resource_bundles = {
'MainMoudle' => ['MainMoudle/Assets/*']
}
复制代码
修改完配置信息和图片记得执行
pod install
把资源文件导入到项目中
在xib
中加载图片, 须要在图片前面加上组件的主bundle
, 相似: MainMoudle.bundle/tabbat_back
私有库中使用代码加载图片, 必定不能使用imageNamed
方法
//1. 获取当前的bundleName
NSBundle *currentBundle = [NSBundle bundleForClass:[self class]];
//2. 根据图片名称, 在bundle中检索图片路径
NSString *path = [currentBundle pathForResource:@"tabbar_np_play@2x.png" ofType:nil inDirectory:@"MainMoudle.bundle"];
//获取图片
UIImage *image = [UIImage imageWithContentsOfFile:path];
复制代码
引用图片须要注意的的是
@2x和@3x
的图片, 因此必须手动指定具体的图片名称包括图片后缀名pathForResource
, 也要必须指明图片所在的bundle
路径, 即inDirectory
参数不可为空提交本地的私有库索引
MainMoudle
中引用了TitanFMBase/Category
部分spec
验证, 不然可能回报错, 缘由只是由于spec
中默认的依赖库是共有的索引库, 私有库没法检索到, 错误信息以下图// 提交本地私有索引库须要忽略警告的命令
pod repo push TitanjunSpec MainMoudle.podspec --allow-warnings
复制代码
欢迎您扫一扫下面的微信公众号,订阅个人博客!