iOS-组件化开发- 远程私有库

创建远程私有管理库

  • 在远程服务器(例:码云、Coding等)创建私有管理库

    就是新建一个私有项目

  • 本地添加私有管理库

    • $ pod repo add HQSpecs git@gitee.com:H-Joe/HQSpecs.git

      7.png

创建私有库(组件)

  • 创建私有库(组件)

    • 创建模版库:$ pod lib create HQBase

      使用模版库的好处是,会自动生成测试工程,可以边开发库边测试。

      如果创建不成功,可能原因之一是Xcodecocoapods版本不匹配,升级cocoapods$ sudo gem install cocoapods

    • 设置配置信息


      8.png

      9.png
    • 生成的模版如下:


      10.png
    • 把要做成私有库(组件)的代码放入Classes中,把其他资源文件(图片、文件等)放在Assets

      11.png

    • 在模版中的测试工程Example中,已经配置好了Podfile,只需pod install即可

      12.png

    1. 如果私有库(组件)中有依赖系统库,则需配置s.frameworkss.library
    2. 如果私有库(组件)中有依赖第三方库,类似SDWebImageAFNetworking等,需要在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.png

      20.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

私有库(组件)中资源文件的使用

    1. xib
    • 在私有库(组件)中加载xib文件需注意以下几点:

        1. 所有使用bundle的地方都必须动态获取
        [NSBundle bundleForClass:[self class]] 
        
        27.png
        1. 不能使用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];
      
    1. 图片
    • 图片资源放置的位置是Assets

      30.png

    • 配置HQMain.podspec文件

      31.png

    • 在私有库(组件)中,图片资源的存放位置和xib文件一样,已经不在mainBundle下,是在xxx.framework下的xxx.bundle

      32.png
    • 使用

        1. xib中使用
        • 在图片名称前面添加组件主bundle, 本例中就是HQMain.bundle
        33.png
         UILabel * 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];
        
        1. 代码加载
        • 不能使用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.pngaixin@2x.pngaixin@3x.png

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容