美文网首页iOS点点滴滴CocoaPodiOS 组件化开发
Cocoapods使用私有库中遇到的坑

Cocoapods使用私有库中遇到的坑

作者: 雷侯塞利 | 来源:发表于2017-01-14 10:12 被阅读13020次

以前使用Cocoapods时候就是简单的创建Podfile,然后执行pod install就完事。最近临近过年了空闲时间比较多就琢磨把项目中复用比较多的代码抽取出来然后用Cocoapods管理,算是迈开项目组件化的第一步。途中遇到若干坑,这里就总结下方便自己日后查看。

这里先给出简单使用私有库的教程链接:
Cocoapods 应用第二部分-私有库相关

1.pod lib lint 和 pod spec lint 命令的区别

pod lib lint是只从本地验证你的pod能否通过验证
pod spec lint是从本地和远程验证你的pod能否通过验证
我一般都是直接使用pod spec lint去验证pod有没有问题

2.私有pod的验证

使用pod spec lint去验证私有库能否通过验证时应该,应该要添加--sources选项,不然会出现找不到repo的错误

pod spec lint --sources='私有仓库repo地址,https://github.com/CocoaPods/Specs'

3.subspec

为了让自己的Pod被导入时显示出良好的文件层划分,subspec是必须的。
若subspec要依赖其它的subspec,则subspec的dependency后面接的不是目录路径,而是specA/specB这种spec关系

4.私有库引用私有库的问题

在私有库引用了私有库的情况下,在验证和推送私有库的情况下都要加上所有的资源地址,不然pod会默认从官方repo查询。

pod spec lint --sources='私有仓库repo地址,https://github.com/CocoaPods/Specs'
pod repo push 本地repo名 podspec名 --sources='私有仓库repo地址,https://github.com/CocoaPods/Specs'

5.引用自己或第三方的framework或.a文件时
在podsepc中应该这样写:

s.ios.vendored_frameworks = "xxx/**/*.framework"
s.ios.vendored_libraries = "xxx/**/*.a”

5.便捷地开发本地私有库

Cocoapods就提供了一个开发模式,其实操作起来也是非常简单的事情,就是将所谓的引用路径修改成本地路径即可。就是讲Podfile中的pod '库名', :path => '本地路径'即可。这样在通常的修改代码中是不需要执行pod update的,但是对于如果修改了目录结构(添加、删除或者移动文件文件)或者是修改了Podspec文件的配置的话,最好是运行一下pod update的命令。普通修改代码的情况下就不需要运行pod update命令和打tag了。
pod 'iOS-Test', :path => '../iOS-Test’

6.私有库中添加资源(图片、音视频等)

方法共有三种:

  • 第一种
spec.resources = ["Images/*.png", "Sounds/*"]

但是这些资源会在打包的时候直接拷贝的app的Bundle中,这样说不定会和其它资源产生命名冲突

  • 第二种
spec.resource = "Resources/MYLibrary.bundle"

把资源都放在bundle中,然后打包时候这个bundle会直接拷贝进app的mainBundle中。使用的时候在mainBundle中查找这个bundle然后再搜索具体资源

    NSURL *bundleURL = [[NSBundle mainBundle] URLForResource:@"JZShare" withExtension:@"bundle"];
    NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
    UIImage *img = [UIImage imageNamed:icon inBundle:bundle compatibleWithTraitCollection:nil];
  • 第三种
spec.resource_bundles = {
  'MyLibrary' => ['Resources/*.png'],
  'OtherResources' => ['OtherResources/*.png']
}

这种方法利用 framework 的命名空间,有效防止了资源冲突。
使用方法是先拿到最外面的 bundle,然后再去找下面指定名字 的 bundle 对象,再搜索具体资源

NSBundle *bundle = [NSBundle bundleForClass:[MYSomeClass class]];
NSURL *bundleURL = [bundle URLForResource:@"MyLibrary" withExtension:@"bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithURL: bundleURL];
UIImage *img = [UIImage imageNamed:icon inBundle:bundle compatibleWithTraitCollection:nil];

7.如果私有库添加了静态库或者dependency用了静态库

那么执行pod lib lint还有pod spec lint时候需要加上—user-libraries选项
否则会出现'The 'Pods' target has transitive dependencies错误

8.如果私有库只引用其他库的subspec

只需要依赖想依赖的subspec,不用管主spec(因为依赖subspec必然要依赖主spec)

9.私有库已经通过验证并传到私有repo也能通过pod search,但是就是pod install失败。

这时候只要执行pod update 然后去喝杯水就好了。。。(前提是你把官方源换成国内的,不然从github上更新官方repo的速度你懂的。 更换官方源

参考

相关文章

网友评论

  • sydie:我编译了一个.a文件放入私有库, - NOTE | [iOS] xcodebuild: ld: warning: ignoring file justgo_QRCodeRideModule/justgo_QRCodeRideModule/justgo_QRCodeRideModule/SDK/libJiangsu-UNION-QRBUS-SDK.a, missing required architecture i386 in file justgo_QRCodeRideModule/justgo_QRCodeRideModule/justgo_QRCodeRideModule/SDK/libJiangsu-UNION-QRBUS-SDK.a (2 slices)。是不是.a必须要导入i386的才行?
  • 翀鹰精灵:当我执行pod lib create <私有库名字> 这个命令的时候,不能创建私有库的模板,报下面的错误:
    /Library/Ruby/Gems/2.3.0/gems/xcodeproj-1.5.8/lib/xcodeproj/project/object/helpers/build_settings_array_settings_by_object_version.rb:27:in `<module:BuildSettingsArraySettingsByObjectVersion>': undefined method `to_set' for #<Array:0x007f925d88dc30> (NoMethodError)
    Did you mean? to_s
    from /Library/Ruby/Gems/2.3.0/gems/xcodeproj-1.5.8/lib/xcodeproj/project/object/helpers/build_settings_array_settings_by_object_version.rb:8:in `<class:XCBuildConfiguration>'
    from /Library/Ruby/Gems/2.3.0/gems/xcodeproj-1.5.8/lib/xcodeproj/project/object/helpers/build_settings_array_settings_by_object_version.rb:4:in `<module:Object>'
    翀鹰精灵:from /Library/Ruby/Gems/2.3.0/gems/xcodeproj-1.5.8/lib/xcodeproj/project/object/helpers/build_settings_array_settings_by_object_version.rb:3:in `<class:Project>'
    from /Library/Ruby/Gems/2.3.0/gems/xcodeproj-1.5.8/lib/xcodeproj/project/object/helpers/build_settings_array_settings_by_object_version.rb:2:in `<module:Xcodeproj>'
    from /Library/Ruby/Gems/2.3.0/gems/xcodeproj-1.5.8/lib/xcodeproj/project/object/helpers/build_settings_array_settings_by_object_version.rb:1:in `<top (required)>'
    from /Library/Ruby/Site/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Library/Ruby/Site/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Library/Ruby/Gems/2.3.0/gems/xcodeproj-1.5.8/lib/xcodeproj/project/object/build_configuration.rb:194:in `<top (required)>'
    from /Library/Ruby/Site/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Library/Ruby/Site/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Library/Ruby/Gems/2.3.0/gems/xcodeproj-1.5.8/lib/xcodeproj/project/object.rb:521:in `<top (required)>'
    from /Library/Ruby/Site/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Library/Ruby/Site/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Library/Ruby/Gems/2.3.0/gems/xcodeproj-1.5.8/lib/xcodeproj/project.rb:5:in `<top (required)>'
    from /Library/Ruby/Site/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Library/Ruby/Site/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/allison/Desktop/founderSpec/setup/ProjectManipulator.rb:30:in `run'
    from /Users/allison/Desktop/founderSpec/setup/ConfigureiOS.rb:73:in `perform'
    from /Users/allison/Desktop/founderSpec/setup/ConfigureiOS.rb:7:in `perform'
    from /Users/allison/Desktop/founderSpec/setup/TemplateConfigurator.rb:85:in `run'
  • 牧羊人Q:私有库可以搜索到但是安装的时候提示没有没有找到 Unable to find a specification for `QYBaseLibrary (~> 0.1.0)`楼主知道吗
    GXL_CR:请问下你是怎么解决的
  • 晨煜煌:你好, 我在依赖第三方的时候,第三方有.a,用--use-libraries,但是用了之后又会报错说找不到.a中的文件,怎么解决?
  • 夢若繁星:自己的私有库依赖于其他自己的基础组件,pod spec lint 加了路劲仍然找不到那个基础库问题有人解决了吗?
    赵文彬_:我加了自己的路径,但是验证的时候还是从master repo去克隆,请问你解决了吗?
  • 夢若繁星:可以加个好友么 有个问题解决不了 ``私有库引用私有库的问题.
  • 海浪萌物:私有库中添加资源那个,我在自己私有库中添加图片,怎么都显示不出来怎么回事呢
    海浪萌物:@chokshen 没解决。。。
    chokshen:兄弟,我也遇到了,怎么解决的?
  • MaricleZhang:—user-libraries 应该为--use-libraries
  • Casablanca1Q84S:你好,上传的pods 项目里面引用了其他私有库,都是公司内的小伙伴自己写的,那么我改怎么在pods spec 文件里面配置呢?
  • 2c68a794e6c3:我碰到过一个问题,我周五传上去的然后在自己的电脑上能pod search 可以搜索到,客户那边死活就是搜索不到。到了周一居然丫的又能搜索到了!难道是时差?还是pod有审核机制
    iWe:pod install, pod search 都是在本地资源下进行索引的,所以你提交之后,需要用pod repo update一次本地库才行
  • 日落夕阳黄昏:pod spec lint 失败咋回事 The spec did not pass validation, due to 3 warnings (but you can use `—allow-warnings` to ignore them).

    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.3.1/lib/cocoapods/command/spec/lint.rb:87:in `run'
    /Library/Ruby/Gems/2.3.0/gems/claide-1.0.2/lib/claide/command.rb:334:in `run'
    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.3.1/lib/cocoapods/command.rb:52:in `run'
    /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.3.1/bin/pod:55:in `<top (required)>'
    /usr/local/bin/pod:23:in `load'
    /usr/local/bin/pod:23:in `<main>'
    夢若繁星:这个你解决了么``警告可以忽略
  • _晨跑:依赖其他subspec ,dependency 写 xxx 还是 xxx.spec
  • 着魔的毛豆:如何提高pod repo push的速度,每次提交都特别的慢,特别当私有库大了之后。
  • 着魔的毛豆:如何提高pod repo push的速度,每次提交都特别的慢,特别当私有库大了之后。
  • Lee_007:你好,想请教一个问题,在做私有库时,使用了oc和swift混编,
    oc中使用【#import "工程名-Swift.h"】导入了swift文件,
    podspec文件中使用【user_target_xcconfig = { 'SWIFT_OBJC_INTERFACE_HEADER_NAME' => '工程名-Swift.h' }】
    可是验证的时候一直报找不到'工程名-Swift.h'
  • Jovins:怎样删掉已经提交的描述文件?
  • brownfeng:刚刚尝试了资源.那里的第三种方法,在pod的example中可行.但是如果使用pod package打包成x.framework,然后引入到工程项目中就不行. 将framework addtarget以后,可以看到只有Header文件夹引入了.而Resource文件夹并没有引入到工程,因此里面的bundle也没有引入.会导致crash.报错找不到bundle
    brownfeng:@搬砖的菜鸡 bundle 资源拖出来. 不用pod 加入到工程中
    刻舟求鉴::scream: 层主最后怎么是解决的呢。。。我也第三种打包成framework 别人pod install 之后会crash 路径nil
  • brownfeng:资源那里很实用
  • d83b1a9a55db:关于私有库的相互依赖有点问题想请教一下
    d83b1a9a55db:我是在podspec文件中添加私有库的依赖出现的问题,验证通不过,总是说找不到那个私有库的None of your spec sources contain a spec satisfying the dependency: `MYPlayerSDK (= 0.0.1)
    现在正在pod repo upde中,方便的话加一下qq吧
    雷侯塞利:https://github.com/CocoaPods/CocoaPods/issues/4921
    How can i add the private dependency to podspec file?
    在podspec中依赖私有repo
    雷侯塞利:@浅忆微凉iOS 什么问题? 看看能不能帮到你
  • 肆意二货:spec.dependency依赖库,我在pod私有库的时候会不会下载下来?
    雷侯塞利:应该会下载下来吧 难道你的不会下载下来吗?
  • Archerlly:多谢, 解燃眉之急!

本文标题:Cocoapods使用私有库中遇到的坑

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