iOS 私有庫(kù)創(chuàng)建遇到的問題和podspec文件的編寫

關(guān)于私有庫(kù)的搭建可查看

1.pod lib create失敗

提示:

Failed to connect to raw.githubusercontent.com port 443: Connect

查看解決

2.pod repo push失敗

刪除本地倉(cāng)庫(kù),重新添加接口,執(zhí)行:

pod repo remove mySpecs
pod repo add mySpecs https://github.com/*****/MySpecs
3.bitcode支持

配置.podspec文件:

s.xcconfig = {'ENABLE_BITCODE' => 'NO'}

這個(gè)主要是引入組件庫(kù)內(nèi)包含了不支持bitcode的二進(jìn)制文件

4.依賴系統(tǒng)類庫(kù),如tdb、dylib

依賴frameworks,配置.podspec文件:

s.frameworks = 'AVFoundation', 'SystemConfiguration'

依賴libc++.dylib等文件,這里需要去掉頭尾的lib、dylib等,配置.podspec文件:

s.libraries = 'c++','z'
5.依賴第三方庫(kù)

配置.podspec文件:

s.dependency 'ZFPlayer/AVPlayer'

依賴的第三方庫(kù)用到了靜態(tài)庫(kù),還需要添加:

 s.static_framework = true
6.依賴自己的私有庫(kù)

配置.podspec文件:

// 例
s.dependency 'ZFPlayer'

這里需要注意一下,在進(jìn)行私有庫(kù)驗(yàn)證pod lib lintpod spec lint時(shí),還需要加上source,多個(gè)用逗號(hào)分隔,其中必須要包含github的source:

pod lib lint --allow-warnings --sources='https://github.com/*****/MySpecs,https://github.com/CocoaPods/Specs.git'
pod spec lint --allow-warnings --sources='https://github.com/*****/MySpecs,https://github.com/CocoaPods/Specs.git'

在執(zhí)行pod repo push的時(shí)候,也需要加上對(duì)應(yīng)的source。如果執(zhí)行上述命令還是失敗,還需要加入一下參數(shù),不用全部加入,混合使用即可,直到執(zhí)行成功:

--no-clean
--allow-warnings
--use-libraries
--verbose
--skip-import-validation

如:

pod lib lint --no-clean --verbose  --allow-warnings --sources='https://github.com/*****/MySpecs,https://github.com/CocoaPods/Specs.git'
pod spec lint --no-clean --verbose  --allow-warnings --sources='https://github.com/*****/MySpecs,https://github.com/CocoaPods/Specs.git'
7.i386報(bào)錯(cuò)提示

配置.podspec文件:

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

其中x86_64即i386,是用于模擬器的芯片指令集架構(gòu)文件

8.按條件文件路徑引入

類似shareSDK的`pod 'mob_sharesdk/ShareSDKPlatforms/WeChat',實(shí)現(xiàn)類似精簡(jiǎn)版、完整版的SDK

如圖在Classes下分不同的文件夾:

之后配置.podspec文件,子模塊也可以設(shè)置不同的依賴,如:

  s.default_subspec = 'ImageView'
      
  # 基礎(chǔ)功能配置
  s.subspec 'ImageView' do |imageView|
      imageView.source_files = 'MyImageView/Classes/ImageView/**/*'
      imageView.public_header_files = 'MyImageView/Classes/ImageView/**/*.h'
      
      imageView.frameworks = 'AVFoundation'
  end
      
  # 附加功能配置
  s.subspec 'GifImageView' do |gifImageView|
      gifImageView.source_files = 'MyImageView/Classes/GifImageView/**/*.{h,m}'
      gifImageView.public_header_files = 'MyImageView/Classes/GifImageView/**/*.h'
      gifImageView.dependency 'MyImageView/ImageView'

      gifImageView.resource_bundles = {
          'MyImageView' => ['MyImageView/Assets/*']
      }
  end
9.添加圖片資源

這里只包括添加圖片的其中一種方式,就是將編輯完成的圖片資源放入Assets文件加下,之后配置.podspec文件:

s.resource_bundles = {
  'MyImageView' => ['MyImageView/Assets/*']
}

這里圖片的獲取也不能用常規(guī)的[UIImage imageNamed:@""]方式了,而是要換成:

- (UIImage *)getImageWithName:(NSString *)nameString{
    NSURL *associateBundleURL = [[NSBundle mainBundle] URLForResource:@"Frameworks" withExtension:nil];
    associateBundleURL = [associateBundleURL URLByAppendingPathComponent:@"MyImageView"];
    associateBundleURL = [associateBundleURL URLByAppendingPathExtension:@"framework"];
    NSBundle *associateBunle = [NSBundle bundleWithURL:associateBundleURL];
    associateBundleURL = [associateBunle URLForResource:@"MyImageView" withExtension:@"bundle"];
    NSBundle *bundle = [NSBundle bundleWithURL:associateBundleURL];
    /// 這里去區(qū)分下用哪種像素的圖片
    NSInteger scale = [[UIScreen mainScreen] scale];
    NSString *imgName = [NSString stringWithFormat:@"%@@%zdx.png", nameString,scale];
    UIImage *image = [UIImage imageWithContentsOfFile:[bundle pathForResource:imgName ofType:nil]];
    return image;
}

這里需要注意一下,此方法最好不要用在category中,如果使用到了,請(qǐng)將其中的方法名根據(jù)不同的組件做區(qū)分,否則會(huì)存在圖片無(wú)法顯示之類的問題

10.將組件打包成framework

這里拿opencv的framework做一下演示,將打包好的framework拖入組件工程,如圖:

文件命名建議和framework的命名一致,方便閱讀,使用的時(shí)候如下:

// 前半部分framework名稱,后半部分為framework內(nèi)部的某個(gè).h文件,可以將頭文件都放在Opencv.h下
#import <opencv2/Opencv.h>

你可以只做framework的組件(如bugly、shareSDK等),但是使用者只能根據(jù)你的文檔接入。你也可以在framework的基礎(chǔ)上加上代碼部分(如Jpush、TalkingData等),方便使用者理解。

類似的第三方庫(kù)你可以多看看,合理規(guī)劃好自己的組件開發(fā)方案,下面開始配置.podspec文件:

# 自己的framework,這里需要注意下路徑是否正確
s.ios.vendored_frameworks = 'OpenCV/opencv2.framework'
10.在Other Linker Flags添加配置項(xiàng)
s.xcconfig = { "OTHER_LDFLAGS" => "-ObjC"}
11.設(shè)置Allow Non-modular includes in Framework Modules為YES
s.user_target_xcconfig = { 'CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES' => 'YES' }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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