美文网首页
iOS开发组件化开发cocoapods私有库制作

iOS开发组件化开发cocoapods私有库制作

作者: 小堆同学 | 来源:发表于2019-10-24 23:15 被阅读0次

前言

最近公司准备开发一个模块,在App首页添加个入口.想到公司的项目已经很庞大了,每次改一点东西都要整个工程运行,很浪费时间,同时考虑到后期其他定制类的项目有可能也要用到这个模块,又要粘贴复制(前面一直这样做的,很恶心).所以就想这把这个模块作为私有库,想用的时候直接pod下载.

制作过程

  1. 创建代码私有库
    去GitHub创建一个代码库(可以用自己公司的git服务),创建的时候新建一下LICENSE文件MIT类型, 然后clone下来,比如我创建的地址是:https://github.com/scrumsnail/HLInterTestPod.git
    然后把自己模块代码push上去.终端命令是
git add .
git commit -m 'log日志'
git pull
git push

这是我们平常代码控制操作.

  1. 创建私有podspec文件
    找到自己刚刚clone下来的文件,cd进去,然后执行
pod spec create HLInterTestPod

HLInterTestPod这个是你要创建的私有库名字.然后你会发现生成了一个.podspec文件,用文本编辑打开,根据自己情况进行修改,不让pod不会成功,可以全部删掉,然后替换下面内容再进行修改

Pod::Spec.new do |spec|
spec.name         = "HLInterTestPod" # 私有库名称
spec.version      = "1.0.9" # 私有库版本号
spec.summary      = "这是护理的私有库" # 项目简介
spec.description  = <<-DESC
          "this is hull pod"
                 DESC # 项目简介
spec.homepage     = "https://github.com/scrumsnail/HLInterTestPod" # 仓库的主页
spec.license      = "MIT" # 开源许可证
spec.author             = { "luyoudui" => "3269190984@qq.com" } # 作者信息
spec.platform     = :ios, "9.0" #平台及支持的最低版本
spec.source       = { :git => "https://github.com/scrumsnail/HLInterTestPod.git", :tag => "#{spec.version}" } #仓库地址
spec.source_files  = "HLInterTestPod/HLInterTestPod/Classes/**/*" #代码位置,路径一定要弄对
spec.resource_bundles = {
          'HLInterTestPod' => ["HLInterTestPod/HLInterTestPod/Assets/*"]
                           }#资源包,如果私有库中要用到图片和xib等,路径要对
spec.exclude_files = "HLInterTestPod/HLInterTestPod/AppDelegate.{h,m}", "HLInterTestPod/HLInterTestPod/main.m" #过滤文件,这些文件将不会上传
spec.dependency "MBProgressHUD" # 依赖库,每一个依赖都要写
spec.requires_arc = true #是否支持arc

end

  1. 提交代码
    提交之前先验证一下自己创建的podspec文件对不对
pod lib lint

没问题显示 HLInterTestPod passed validation
然后就可以放心的提交代码了

git add .
git commit -m 'log日志'
git tag '1.0.0'  //这里版本号很重要,一定要跟podspec文件中的version相对应
git push --tags //推送到仓库
git push
pod trunk push // 发布podsepc,这里如果没有注册trunk,要先注册,下面关于这个做一个介绍
然后在主工程引用 pod 'HLInterTestPod','~>1.0.9'
  1. pod trunk问题
    没有注册过trunk的,先注册
pod trunk register 3269190984@qq.com 

然后你有邮箱将会收到一条信息,验证一下即可
验证

pod trunk me

添加同事一起维护这个私有库

pod trunk add-owner HLInterTestPod 已注册trunk的邮箱地址

移除某个维护人员

pod trunk remove-owner HLInterTestPod 已注册trunk的邮箱地址

删除已发的某个版本对应的工程信息

pod trunk delete HLInterTestPod 版本号

xib问题

如果想要在主工程中引用私有库中的xib,我们正常写法直接push到控制器,这个时候不会显示xib的.因为这个xib的bundle路径不对.

  • 解决方法
    在主工程写一个分类
//  NSBundle+HLBundle.h
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSBundle (HLBundle)

+ (instancetype)hlBundleWithBundleName:(NSString *)bundleName targetClass:(Class)targetClass;

@end

NS_ASSUME_NONNULL_END
//  NSBundle+HLBundle.m
#import "NSBundle+HLBundle.h"

@implementation NSBundle (HLBundle)


+ (instancetype)hlBundleWithBundleName:(NSString *)bundleName targetClass:(Class)targetClass {
    //并没有拿到子bundle
    NSBundle *bundle = [NSBundle bundleForClass:targetClass];
    //在这个路径下找到子bundle的路径
    NSString *path = [bundle pathForResource:bundleName ofType:@"bundle"];
    //根据路径拿到子bundle
    return path?[NSBundle bundleWithPath:path]:[NSBundle mainBundle];
}
@end

然后调用

HLHomeViewController *vc = [[HLHomeViewController alloc] initWithNibName:@"HLHomeViewController" bundle:[NSBundle hlBundleWithBundleName:@"HLInterTestPod" targetClass:[self class]]];
    [self.navigationController pushViewController:vc animated:YES];

私有库更新

当你的私有库需要修改或者添加东西的时候,一定要去修改podspec的version,比上个version高,然后重复上面制作过程中的第3步提交代码. 每一次执行pod trunk push成功之后,想要在主工程pod的时候你会发现报错,说没有次版本的库.(可能是要审核)

  • 解决方法
    等5分钟左右,执行 pod install --repo-update

以下是我的私有库和主工程,大家可以下载下来对应文档看看,如有不足,请大家补充,欢迎讨论
私有库:https://github.com/scrumsnail/HLInterTestPod

主工程:https://github.com/scrumsnail/MainTestPodRepo

相关文章

网友评论

      本文标题:iOS开发组件化开发cocoapods私有库制作

      本文链接:https://www.haomeiwen.com/subject/fdqjvctx.html