美文网首页iOS开发你需要知道的Cocoapods
CocoaPods私有库可能遇到的坑

CocoaPods私有库可能遇到的坑

作者: Coder_JMicheal | 来源:发表于2018-05-11 13:37 被阅读138次

    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'
    

    引用自己或第三方的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/*"]
    
    • 第二种
    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的速度你懂的。 更换官方源

    相关文章

      网友评论

        本文标题:CocoaPods私有库可能遇到的坑

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