使用pod库要注意的tips

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

  • pod lib 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,则subspecdependency后面接的不是目录路径,而是specA/specB这种spec关系;

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

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

1. pod spec lint --sources='私有仓库repo地址,https://github.com/CocoaPods/Specs'
2. 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"

6.引用静态库:(.ios).library。去掉头尾的lib,用”,”分割

// 引用libxml2.lib和libz.lib.
spec.libraries =  'xml2', 'z'

7.引用公有framework:(.ios).framework. 用”,”分割. 去掉尾部的”.framework

spec.frameworks = 'UIKit','SystemConfiguration', 'Accelerate'

8.引用自己生成的framework:(.ios).vendored_frameworks。用”,”分割,路径写从.podspec所在目录为根目录的相对路径 ps:这个不要省略.framework

spec.ios.vendored_frameworks = 'Pod/Assets/*.framework'

9.引用自己生成的.a文件, 添加到Pod/Assets文件夹里. Demo的Example文件夹里也需要添加一下, 不然找不到

spec.ios.vendored_libraries = 'Pod/Assets/*.a'

在提交到私有仓库的时候需要加上--use-libraries

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

方法共有三种:

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

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

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

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

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

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

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

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

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

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

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

13.提交到私有仓库的之前可以先验证一下, 有问题就修复它, 验证过了在提交

pod spec lint VenderName.podspec --verbose

打好tag, 推到Git里去后, 才可以在测试的项目里的Podfile里引用这个库, 然后pod update VenderName --no-repo-update, 测试通过了, 在提交到私有仓库里

pod 'VenderName', :podspec => 'VenderName.podspec的路径地址'

还可以指定引用某个分支的代码

pod 'VenderName', :git => 'https://git.coding.net/CodingZero/VenderName.git', :branch => 'develop'

提交到私有仓库的时候还可以忽略警告类的错误, 愣是要提交. 在后面加上--allow-warnings

pod repo push LYSpecs VenderName.podspec --allow-warnings

如果有添加新的文件, 需要更新下引索, Demo里才可以识别

pod update VenderName --no-repo-update

14.使用的时候还可以通过直接指定地址 + tag or 分支 or commit 的方式来引入, 这样就可以不用走发布流程了. 也不需要添加源了.

pod 'VenderName', :git => 'https://git.coding.net/CodingZero/VenderName.git', :tag => '0.8.1'

pod 'VenderName', :git => 'https://git.coding.net/CodingZero/VenderName.git', :branch => 'develop'

pod 'VenderName', :git => 'https://git.coding.net/CodingZero/VenderName.git', :commit => '0812fe81319af2411233'

15. 如果 Podfile 中写了更新命名:pod repo update xxxspec。执行后有时候会遇到下面的报错

fatal: cannot change to '/xx/xx/.cocoapods/repos/xxxspec': No such file or directory

可能是你通过命令:

pod repo add NAME URL [BRANCH]

添加 spec repo 的时候 取的名称 NAME 和上面报错地方的xxxspec不对应

所以把 Podfilexxxspec改为你本地命名的 NAME

参考链接:CocoaPods私有库使用Tips

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

推荐阅读更多精彩内容