iOS 创建、发布私有库

240 阅读4分钟

一、创建远程私有索引库

1、创建远程私有索引库(远程仓库gitee),并复制仓库地址(点击克隆/下载),关联到本地。
2、打开终端,将远程私有库关联到本地。
cd /Users/zhanyingzhu/.cocoapods/repos
pod repo add MyLib https://gitee.com/jamace/MyLib.git
3、终端使用pod repo 或 pod repo list就可以看到管理到本地的索引库
MyLib

- Type: git (master)

- URL:  https://gitee.com/zhangkai-Inke/MyLib.git

- Path: /Users/zhanyingzhu/.cocoapods/repos/MyLib

二、创建私有仓库和组件工程

0、创建索引仓库并将其添加到本地

在gitee中创新新项目,拉到本地。

911701755408_.pic.jpg

pod repo add MYSpec https://gitee.com/xxx/test-spec.git
//查看
pod repo

创建自己的私有远程Pod库

1、在码云上创建远程仓库ZHUIKit

之后所有的代码、tag、分支等都在远程仓库这里处理。

2、创建组件工程
2.1 创建本地私有代码库
pod lib create ZHUIKit
2.2 组件工程配置
What platform do you want to use?? [ iOS / macOS ]
>iOS

//开发语言设置,根据自己而定,这里为ObjC
What language do you want to use?? [ Swift / ObjC ]
>ObjC 

//是否需要创建一个demo用来测试你的组件,这里选择Yes,是为了之后对写好的组件进行测试
Would you like to include a demo application with your library? [ Yes / No ]
>Yes 

//测试框架
Which testing frameworks will you use? [ Specta / Kiwi / None ]
>None 

//是否要做基础的视图测试
Would you like to do view based testing? [ Yes / No ]
>No 

//文件前缀
What is your class prefix?
>ZH 
2.3 组件工程目录

Class:类文件放置

Assets:图片、文件放置.注意:获取资源文件需要用Bundle的方式,否则无法获取到。 获取图片:

[_historyBtn setImage:[UIImage city_imageNamed:@"newfindhistoryimg"] forState:UIControlStateSelected];

+ (UIImage *)city_imageNamed:(NSString *)imageName {
    return [UIImage imageWithTYTBundleName:@"TYTCitySelectedView" imageNamed:imageName];
}
+ (UIImage *)imageWithTYTBundleName:(NSString *)bundleName imageNamed:(NSString *)name
{
    static NSString *resourceType = @"bundle";
    NSBundle *mainBundle = [NSBundle bundleWithIdentifier:[TYTAppVersion appBundleIdentifier]];
    return [UIImage imageNamed:name inBundle:[NSBundle bundleWithPath:[mainBundle pathForResource:bundleName ofType:resourceType]] compatibleWithTraitCollection:nil];
}
+ (NSString *)appBundleIdentifier
{
    if (!appBundleIdentifier) {
        appBundleIdentifier = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
    }
    return appBundleIdentifier;
}

获取 json文件

NSString *filePatch = [[NSBundle mainBundle] pathForResource:@"citydataselect" ofType:@"xml"];

    NSData *reader = [NSData dataWithContentsOfFile:filePatch];

    NSError *error;

    NSDictionary *dict  = [SskyXMLParser dictionaryForXMLData:reader error:error];

image.png

三、podspec配置

#

# Be sure to run `pod lib lint TYTCitySelectedView.podspec' to ensure this is a

# valid spec before submitting.

#

# Any lines starting with a # are optional, but their use is encouraged

# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html

#

  


Pod::Spec.new **do** |s|

  s.name             = 'TYTCitySelectedView'

  s.version          = '0.1.0'

  s.summary          = '城市选择组件'

  s.description      = <<-DESC

  s.homepage         = 'git@192.168.2.20:iOS-Components/TYTCitySelectedView.git'

  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'

  s.license          = { :type => 'MIT', :file => 'LICENSE' }

  s.author           = { 'zhangsan' => '“1111111111@qq.com”' }

  s.source           = { :git => 'git@192.168.2.20:iOS-Components/TYTCitySelectedView.git', :tag => s.version.to_s }

  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
  s.ios.deployment_target = '10.0'
  s.source_files = 'TYTCitySelectedView/Classes/**/*'

  s.resources = 'TYTCitySelectedView/Assets/Other/*'
   //设置bundle的路径
   s.resource_bundles = {
     'TYTCitySelectedView' => ['TYTCitySelectedView/Assets/*.xcassets']
   }

   s.static_framework = true

  # s.public_header_files = 'Pod/Classes/**/*.h'

   //依赖frameworks
  # s.frameworks = 'UIKit', 'MapKit'
  
  //设置依赖的三方库
  s.dependency 'AFNetworking', '~> 2.3'

  s.dependency 'MJExtension' , '3.4.1'

  s.dependency 'TYTFoundation'

  s.dependency 'Masonry','1.1.2'

  s.pod_target_xcconfig = {
      'VALID_ARCHS' => 'x86_64 armv7 arm64'
  }

  s.user_target_xcconfig = {
      'GENERATE_INFOPLIST_FILE' => 'YES'
  }

end

podspec文件介绍

四、组件的提交发布

1、提交代码后,打tag

注意:这里的tag号必须和.podSpec文件的版本号一致

git tag -a "0.0.2-beta" -m "1、 此版本为0.1.0版本的beta版,上传相关类和依赖。" 
git push --tags
2、对文件进行本地验证和远程验证(在工程目录下)
2.1 从本地验证你的pod能否通过验证
两种方式:
pod lib lint --use-libraries --allow-warnings
pod lib lint --use-libraries --allow-warnings --verbose --sources=MyLib

1.--verbose:有些非语法错误是不会给出错误原因的,这个时候可以使用--verbose来查看详细的验证过程来帮助定位错误。

2.--use-libraries:表示使用静态库或者是framework,这里主要是解决当我们依赖一些framework库后校验提示找不到库的时候用到。

3.--allow-warnings:表示允许警告。

2.2 从本地和远程验证的pod能否通过验证
方法一:
pod spec lint --use-libraries --allow-warnings
方法二:如果有相应的报错,可以用这种方法,去除所有的警告、可以用modular
pod spec lint --verbose --allow-warnings --use-libraries --no-clean --sources=MyLib --use-modular-headers
2.3 将spec 文件提交到本地的私有仓库,然后再push到远程仓库
pod repo push MyLib ZHUIKit.podspec --verbose --use-libraries --allow-warnings 
2.4 更新repo本地源

提交成功之后,更新本地源,否则会报错找不到版本。

pod repo update MyLib

image.png

2.5 更新组件使用

pod install或者pod update ZHUIKit --no-repo-update即可。

五、优化发布流程

编写.sh脚本文件(是UNIX/LINUX操作系统的脚本文件),直接执行sh文件,简化我们的教研和发布代码。sources后面是应用的相关的源地址。

1、校验脚本
pod lib lint --verbose --sources=https://gitee.com/mirrors/CocoaPods-Specs.git,git@192.168.2.20:cocoapods/TYT-Specs.git,git@code.amh-group.com:iOSYmm/mb-specrepo.git,git@code.amh-group.com:cocoapods/mb-thirdpartyspecrepo.git --allow-warnings --use-libraries --use-modular-headers
2、push脚本
pod repo push git@192.168.2.20:cocoapods/TYT-Specs.git *.podspec --sources=https://gitee.com/mirrors/CocoaPods-Specs.git,git@192.168.2.20:cocoapods/TYT-Specs.git,git@code.amh-group.com:iOSYmm/mb-specrepo.git,git@code.amh-group.com:cocoapods/mb-thirdpartyspecrepo.git --allow-warnings --use-libraries --use-modular-headers --verbose
3、执行脚本

提交前的准备完毕之后,执行这两个脚本就可以了。

sh pod_lint.sh
sh pod_push.sh

# iOS组件化的创建、tag、提交到远端