上篇文章[Cocoapods]项目添加Cocoapods支持主要介绍了添加Cocoapods支持的大致过程, 当然文章看上去是一帆风顺的. 但是, 事实并不是这样. 上篇文章篇幅过长, 就把之间遇到的一些问题, 单独写了出来.
1. ERROR | spec: The specification defined in `LZTool.podspec` could not be loaded.
MacBook:PodTest Artron_LQQ$ pod lib lint LZTool.podspec
-> LZTool.podspec
- ERROR | spec: The specification defined in `LZTool.podspec` could not be loaded.
[!] Invalid `LZTool.podspec` file: syntax error, unexpected tIDENTIFIER, expecting keyword_end
...<-DESC 这是一个测试文档, 这里主要是描述类库...
... ^
LZTool.podspec:28: syntax error, unexpected '\n', expecting '='.
# from LZTool.podspec:27
# -------------------------------------------
# # * Finally, don't worry about the indent, CocoaPods strips it!
> s.description = <<-DESC 这是一个测试文档, 这里主要是描述类库的功能设计初衷介绍
# DESC
# -------------------------------------------
[!] LZTool.podspec did not pass validation, due to 1 error.
[!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a `.swift-version` file to set the version for your Pod. For example to use Swift 2.3, run:
`echo "2.3" > .swift-version`.
You can use the `--no-clean` option to inspect any issue.
MacBook:PodTest Artron_LQQ$
这个错误是使用指令pod lib lint LZTool.podspec 检查文件是否合法时发生的;
可以看出是在设置s.description 字段时发生的错误, 当时的写法是:
s.description = <<-DESC LZTool 是一个用于保存一些常用工具类的工具 DESC
以为可以吧他们并到一行, 导致一直报这个错, 后来写成两行, 还是报错, 最后才试出这样写才对:
s.description = <<-DESC
LZTool 是一个用于保存一些常用工具类的工具
DESC
2. [iOS] file patterns: The `source_files` pattern did not match any file.
MacBook:PodTest Artron_LQQ$ pod lib lint LZTool.podspec
-> LZTool (0.0.1)
- WARN | github_sources: Github repositories should end in `.git`.
- ERROR | [iOS] file patterns: The `source_files` pattern did not match any file.
[!] LZTool did not pass validation, due to 1 error and 1 warning.
[!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a `.swift-version` file to set the version for your Pod. For example to use Swift 2.3, run:
`echo "2.3" > .swift-version`.
You can use the `--no-clean` option to inspect any issue.
MacBook:PodTest Artron_LQQ$
这个错误也是使用指令pod lib lint LZTool.podspec 检查文件是否合法时发生的;
这个是在指定共享的类库时, 文件路径不对, 也就是设置s.source_files 字段时, 发生了错误, 这里的路径是相对于LZTool.podspec文件的, 如果是与LZTool.podspec同级的文件夹, 直接写文件夹名称即可, 如:
s.source_files = "LZTool"
这里也可以这么写:
s.source_files = "LZTool/*.{h,m}"
如果同级文件夹内有子文件夹,需要这么写:
s.source_files = "LZTool/**/*.{h,m}"
这样会加载同级文件夹内的所有文件,包括子文件夹内的文件;
如果只需要加载某个子文件夹目录下的文件, 一定要逐级添加.
s.source_files = "LZTool", "LZTool/obj/*.{h,m}"
如果,在某个版本的tag下报此错误,更改为正确路径后,还是报此错误,可尝试新建一个新的tag;
3. fatal: Remote branch 0.0.1 not found in upstream origin
MacBook:PodTest Artron_LQQ$ pod spec lint
-> LZTool (0.0.1)
- ERROR | [iOS] unknown: Encountered an unknown error ([!] /usr/bin/git clone https://github.com/LQQZYY/PodTest.git /var/folders/14/95vmx0495_s5292ltvwpsc8h0000gn/T/d20170111-11240-1l3iq9n --template= --single-branch --depth 1 --branch 0.0.1
Cloning into '/var/folders/14/95vmx0495_s5292ltvwpsc8h0000gn/T/d20170111-11240-1l3iq9n'...
warning: Could not find remote branch 0.0.1 to clone.
fatal: Remote branch 0.0.1 not found in upstream origin
) during validation.
Analyzed 1 podspec.
[!] The spec did not pass validation, due to 1 error.
[!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a `.swift-version` file to set the version for your Pod. For example to use Swift 2.3, run:
`echo "2.3" > .swift-version`.
MacBook:PodTest Artron_LQQ$
这个是使用指令pod spec lint来检查文件是否可用时发生的错误;
这是因为在你托管代码的库里(这里是指github)找不到这个分支, 也就是在编辑 LZTool.podspec 时, 里面的字段 s.verson, s.source 中的 tag与github创建的release版本号不一致导致, 修改为一样即可!
这里还有另外一个解决方法方法二
4. 加载xib问题
PS: 这个不是我个人遇到的问题, 是在文章中看到, 这里记录一下;
如果通过cocoapods下载的类库中含有Xib文件, 使用原来的方式初始化就不起作用了:
[[[NSBundle mainBundle] loadNibNamed:@"xibName" owner:self options:nil] lastObject];
[self.collectionView registerNib:[UINib nibWithNibName:@"xibName" bundle:nil] forCellWithReuseIdentifier:@"ZLCollectionCell"];
应该使用下面这种方式初始化:
#define kZLPhotoBrowserBundle [NSBundle bundleForClass:[self class]]
[[kZLPhotoBrowserBundle loadNibNamed:@"ZLPhotoActionSheet" owner:self options:nil] lastObject];
[self.collectionView registerNib:[UINib nibWithNibName:@"ZLCollectionCell" bundle:kZLPhotoBrowserBundle] forCellWithReuseIdentifier:@"ZLCollectionCell"];
这样就能正常使用了;
5.加载图片资源问题
PS: 这个问题我尝试了一个demo, 直接设置了s.resources = "LZScaner/images/*.png" , 图片也能正常显示( 可参考我设置的podspec文件LZScaner.podspec )没有遇到这个问题, 但是还是把这个方式记录一下吧...
如果通过代码"[UIImage imageNamed:@"picName"]" 去设置图片的话,则图片资源有可能无法正常显示, 可通过以下方式解决:
- 创建bundle资源目录
command+N -> Resource -> Settings Bundle
删除bundle携带的无用文件,把图片资源添加到bundle资源内
- 改变代码图片路径
// 图片路径
#define kZLPhotoBrowserSrcName(file) [@"ZLPhotoBrowser.bundle" stringByAppendingPathComponent:file]
#define kZLPhotoBrowserFrameworkSrcName(file) [@"Frameworks/ZLPhotoBrowser.framework/ZLPhotoBrowser.bundle" stringByAppendingPathComponent:file]
kZLPhotoBrowserSrcName(file) 为通过copy文件夹方式获取图片路径的宏
kZLPhotoBrowserFrameworkSrcName(file) 为通过cocoapods下载安装获取图片路径的宏
- 然后修改代码中设置图片的方式如下
UIImage *img = [UIImage imageNamed:kZLPhotoBrowserSrcName(@"img.png")]?:[UIImage imageNamed:kZLPhotoBrowserFrameworkSrcName(@"img.png")];
其podspec地址: ZLPhotoBrowser.podspec 可以参考学习!
6. trunk: getaddrinfo: nodename nor servname provided, or not known
MacBook:LZScaner Artron_LQQ$ pod trunk register [email protected] --description= 'LZScaner'
[!] There was an error registering with trunk: getaddrinfo: nodename nor servname provided, or not known
MacBook:LZScaner Artron_LQQ$
出现这个原因是, 我开了APN, 把APN关了, 重新启动一下网络就好了...
7. xcodebuild: Returned an unsuccessful exit code. You can use `--verbose` for more information
MacBook:LZSortTool Artron_LQQ$ pod spec lint
-> LZSortTool (0.0.1)
- WARN | [iOS] license: Unable to find a license file
- ERROR | [iOS] xcodebuild: Returned an unsuccessful exit code. You can use `--verbose` for more information.
- NOTE | [iOS] xcodebuild: LZSortTool/LZSortToolDemo/LZSortToolDemo/LZSortClass/LZSortTool.m:14:9: fatal error: 'ChineseToPinyin.h' file not found
Analyzed 1 podspec.
[!] The spec did not pass validation, due to 1 error and 1 warning.
[!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a `.swift-version` file to set the version for your Pod. For example to use Swift 2.3, run:
`echo "2.3" > .swift-version`.
MacBook:LZSortTool Artron_LQQ$
这个是执行 pod spec lint指令的时候产生的错误:
这个错误从- NOTE可以看出, 是项目中的这个文件ChineseToPinyin.h找不到, 回到项目, 编译一下也会报这个错. 是因为我修改了本地文件中的这个文件的路径, 项目中引用的还是原先的路径, 打开左侧文件列表, 可以发现这个文件是红色的, 删除引用, 重新添加, 然后到你的github上面, 重新添加一个release版本, 再重新执行指令即可验证通过.
8. 验证成功后搜索不到新加的支持库
如果在制作支持库的过程中没有错误, 或者最好添加库验证通过, 即出现下面这个界面:
在使用
pod search LZTool
的时候, 搜索不到这个库, 或者在使用时找不到这个库, 可以使用下面的指令来清理一下缓存:
rm ~/Library/Caches/CocoaPods/search_index.json
然后再去使用, 基本就可以了.
9. ERROR | name: The name of the spec should match the name of the file.
LQQ-MacBook-Pro:LQWebViewSwift LQiqiang$ pod lib lint LQWebViewSwift.podspec
-> LQWebView-Swift (1.0)
- ERROR | name: The name of the spec should match the name of the file.
- ERROR | [iOS] unknown: Encountered an unknown error (No podspec found for `LQWebView-Swift` in `/Users/LiuQiqiang/Desktop/SourcesTree/Cocoapods/LQWebViewSwift`) during validation.
[!] LQWebView-Swift did not pass validation, due to 2 errors.
You can use the `--no-clean` option to inspect any issue.
LQQ-MacBook-Pro:
出现这个错误是因为,我的podspec 文件名称和里面设置的 ** s.source_files** 和 s.name 不一致导致的,修改为一致的即可!
10. [!] You ([email protected]) are not allowed to push new versions for this pod. The owners of this pod are [email protected].
[!] You ([email protected]) are not allowed to push new versions for this pod. The owners of this pod are [email protected].
这是在执行 pod trunk push LQKit.podspec --allow-warnings时报的错误,这是仓库名称重复了,就修改了一个,再次提交,成功!