iOS-組件化開發(fā)- 遠程私有庫

創(chuàng)建遠程私有管理庫

  • 在遠程服務(wù)器(例:碼云、Coding等)創(chuàng)建私有管理庫

    就是新建一個私有項目

  • 本地添加私有管理庫

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

      7.png

創(chuàng)建私有庫(組件)

  • 創(chuàng)建私有庫(組件)

    • 創(chuàng)建模版庫:$ pod lib create HQBase

      使用模版庫的好處是,會自動生成測試工程,可以邊開發(fā)庫邊測試。

      如果創(chuàng)建不成功,可能原因之一是Xcodecocoapods版本不匹配,升級cocoapods$ sudo gem install cocoapods

    • 設(shè)置配置信息


      8.png

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


      10.png
    • 把要做成私有庫(組件)的代碼放入Classes中,把其他資源文件(圖片、文件等)放在Assets

      11.png

    • 在模版中的測試工程Example中,已經(jīng)配置好了Podfile,只需pod install即可

      12.png

    1. 如果私有庫(組件)中有依賴系統(tǒng)庫,則需配置s.frameworkss.library
    2. 如果私有庫(組件)中有依賴第三方庫,類似SDWebImage、AFNetworking等,需要在HQBase.podspec中添加依賴關(guān)系
      13.png
    • 本地測試
  • 推送到遠程服務(wù)器

    • 在遠程服務(wù)器上創(chuàng)建私有庫(組件)項目


      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
    • 編譯測試通過
  • 推送到遠程服務(wù)器

    • 配置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即可。

子私有庫(組件)

如果私有庫(組件)有很多功能,我們所用的只是其中的一個子功能,就有必要創(chuàng)建子私有庫(組件)

  • 配置HQBase.podspec文件,添加子私有庫(組件)
    • 增加版本號version

    • 設(shè)置依賴dependency

      依賴放在具體的子索引庫中,誰需要,誰設(shè)置,不需要,不設(shè)置

    • 增加subspec

      24.png

    • 本地驗證: $ pod lib lint --allow-warnings

  • 推送到遠程服務(wù)器
    • 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的地方都必須動態(tài)獲取
        [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文件一樣,已經(jīng)不在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.png、aixin@2x.png、aixin@3x.png

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容