安装cocoapods
卸载cocoapods

1.先去git上创建一个仓库XXXkit,clone到本地文件夹Lib/XXXkit下

2.在文件夹temp中,用cocoapods创建XXXkit库

pod lib create XXXkit 

如图
iOS 组件化开发小结

3.将生成的文件copy至文件夹Lib/XXXkit下

iOS 组件化开发小结
这个ReplaceMe.m就是要替换的库文件 删除之后放入你制作的库文件(若有)。

4.cd 到Example文件下 执行

pod install

打开Example工程
iOS 组件化开发小结
在pods中找到 xxxKit.podspec文件修改

iOS 组件化开发小结
若需要分模块 请参考如下代码

 s.subspec 'CoreBundle' do |ss|
   #'CoreBundle'
   ss.public_header_files = 'CoreSupportLib/Classes/CoreBundle/**/*.h'
   ss.source_files = 'CoreSupportLib/Classes/CoreBundle/**/*.{h,m}'
   
 end
 s.subspec 'CoreCategory' do |ss|
   #'CoreCategory'
   ss.public_header_files = 'CoreSupportLib/Classes/CoreCategory/**/*.h'
   ss.source_files = 'CoreSupportLib/Classes/CoreCategory/**/*.{h,m}'
   
 end
 s.subspec 'CoreMacros' do |ss|
   #'CoreMacros'
   ss.public_header_files = 'CoreSupportLib/Classes/CoreMacros/**/*.h'
   ss.source_files = 'CoreSupportLib/Classes/CoreMacros/**/*.{h,m}'
   
 end
 s.subspec 'CoreRouter' do |ss|
   #'CoreRouter'
   ss.public_header_files = 'CoreSupportLib/Classes/CoreRouter/**/*.h'
   ss.source_files = 'CoreSupportLib/Classes/CoreRouter/**/*.{h,m}'

图片和xib的使用请用

pod 'CoreSupportLib/CoreBundle'

NSBundle+txSubBundle
UIImage+txSubBundle

具体代码如下

+(instancetype)tx_subBundleWithBundleName:(NSString *)bundleName targetClass:(Class)targetClass{
    NSBundle *bundle =[NSBundle bundleForClass:targetClass];
    NSString *path =[bundle pathForResource:bundleName ofType:@"bundle"];
    return path?[NSBundle bundleWithPath:path]:[NSBundle mainBundle];
}



  + (instancetype)tx_imgWithName:(NSString *)name bundle:(NSString *)bundleName targetClass:(Class)targetClass{
    NSInteger scale = [[UIScreen mainScreen] scale];
    NSBundle *curB = [NSBundle bundleForClass:targetClass];
    NSString *imgName = [NSString stringWithFormat:@"%@@%zdx.png", name,scale];
    NSString *dir = [NSString stringWithFormat:@"%@.bundle",bundleName];
    NSString *path = [curB pathForResource:imgName ofType:nil inDirectory:dir];
    return path?[UIImage imageWithContentsOfFile:path]:nil;
}

然后

pod  install 

cmd+b 编译

6.确保工程不报错误

可以在主工程Example for XXXKit里测试代码工程
注:当断点无效时请清除工程。product ->clean Build Folder

7.将本地代码推送到远程仓库

利用sourceTree

commit 
添加标签 即 版本号
push

8.在spec所在文件夹下校验 spec(一定记得先要传版本,pod spec lint 只会验证远程仓库的最新版本)

pod spec lint   --use-libraries --allow-warnings --verbose 

当出现如下字样代表校验通过。

iOS 组件化开发小结
–verbose 打印日志
–allow-warnings 允许warn
–use-libraries 依赖库(s.dependency)包含了.a静态库 是验证是无法通过的。可以通过 --use-libraries 来让验证通过。

9.我们现在可以选择推到公共的索引库和私有的索引库

9.1推到公共的索引库

pod trunk register [email protected] 'xx' --description='xx' --verbose

打开验证邮箱收到邮件中链接 确保在spec所在文件夹下

   pod trunk push xx.podspec --use-libraries --allow-warnings --verbose

如果搜不到,不是没传成功,是我们的本地搜索库没更新 。
终端执行

rm ~/Library/Caches/CocoaPods/search_index.json

再执行

pod search xxx

9.2推到私有的索引库

1、私有库/公有库:指的是我们真正放置组件代码的地方。
2、索引库:存放spec文件的地方,用于索引到代码的位置。
打个比方,索引库就好比指针,私有库就好比对象,指针中存放了对象的地址,通过地址可以找到对象!
公共的索引库是指https://github.com/CocoaPods/Specs.git
私有的索引库需要我们自己建
去gitee/github建一个私有库即可
例如https://gitee.com/XXX/txRepos.git
然后

$ pod repo add txRepos https://gitee.com/XXX/txRepos.git

注 删除命令如下加错了可以删除

pod repo remove txRepos

在spec所在文件夹下

pod repo push txRepos xx.podspec --use-libraries --allow-warnings --verbose 

10,使用

在使用的工程里Podfile要加入如下代码才能 pod install

source 'https://github.com/CocoaPods/Specs.git'

source ‘https://gitee.com/XXX/txRepos.git'

其他注意事项

  • 如果组件中含有静态库.a 则需要添加下面代码:
    s.vendored_libraries = ‘xxKit/Classes/xx/*.a’
  • 组件化之后,在主工程调试组件中的代码发现,断点显示变量全是nil
    解决:
    打开Xcode 的Build Setting 搜索 optimization
    修改Debug的对应选项 None[-O0]
  • 若是注册邮箱时没反应 说明网络和电脑不行 后面的push基本也不行

相关文章: