CocoaPods Spec手动下载实践-2020-12-30

原始的Podfile

今天接到了一个使用mPaaS的iOS项目,CocoaPod的配置文件是这样的:

# mPaaS Pods Begin
plugin "cocoapods-mPaaS"
source "https://code.aliyun.com/mpaas-public/podspecs.git"
mPaaS_baseline '10.1.68'  # 请将 x.x.x 替换成真实基线版本
mPaaS_version_code 22   # This line is maintained by MPaaS plugin automatically. Please don't modify.
# mPaaS Pods End
# ---------------------------------------------------------------------
platform :ios, '9.0'
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
# Let's ignore all warnings from all pods
inhibit_all_warnings!


target 'industry' do
  
  mPaaS_pod "mPaaS_TinyApp"
  mPaaS_pod "mPaaS_Push"
  
  pod 'AFNetworking', '~> 4.0.1'
  pod 'MJExtension', '~> 3.1.2'
  pod 'AliyunOSSiOS', '~> 2.10.8'
  pod 'IQKeyboardManager', '~> 6.5.6'
  pod 'MJRefresh', '~> 3.5.0'
  pod 'MGSwipeTableCell', '~> 1.6.11'
  pod 'YYText', '~> 1.0.7'
  pod 'TZImagePickerController', '~> 3.4.8'

end

执行pod install之后,提示了错误:有关mPaaS的内容都找不到

hello:mpaasapp_ios zxs$ pod install

[!] Invalid `Podfile` file: undefined method `mPaaS_pod' for #.

 #  from /Users/zxs/TempProject/mpaasapp_ios/Podfile:17
 #  -------------------------------------------
 #    
 >    mPaaS_pod "mPaaS_TinyApp"
 #    mPaaS_pod "mPaaS_Push"
 #  -------------------------------------------

删除mPaaS相关的内容

除了mPaaS之外,还有一些常用的第三方库,所以先删除mPaaS相关的内容,将其他的第三方库代码下载下来。

# mPaaS Pods Begin
#plugin "cocoapods-mPaaS"
#source "https://code.aliyun.com/mpaas-public/podspecs.git"
#mPaaS_baseline '10.1.68'  # 请将 x.x.x 替换成真实基线版本
#mPaaS_version_code 22   # This line is maintained by MPaaS plugin automatically. Please don't modify.
# mPaaS Pods End
# ---------------------------------------------------------------------
platform :ios, '9.0'
#source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
# Let's ignore all warnings from all pods
inhibit_all_warnings!


target 'industry' do
  
#  mPaaS_pod "mPaaS_TinyApp"
#  mPaaS_pod "mPaaS_Push"
  
  pod 'AFNetworking', '~> 4.0'
  pod 'MJExtension', '~> 3.1.2'
  pod 'AliyunOSSiOS', '~> 2.10.8'
  pod 'IQKeyboardManager', '~> 6.5.6'
  pod 'MJRefresh', '~> 3.5.0'
  pod 'MGSwipeTableCell', '~> 1.6.11'
  pod 'YYText', '~> 1.0.7'
  pod 'TZImagePickerController', '~> 3.4.8'
  
end

下载第三方库

已经删除了mPaaS相关的内容,剩下了普通的CocoaPod下载Spec(本地库)。

  • 执行pod install之后也发生了错误,提示找不到AFNetworking,这就有点搞笑了。
hello:mpaasapp_ios zxs$ pod install
Analyzing dependencies
[!] Unable to find a specification for `AFNetworking (~> 4.0)`

You have either:
 * out-of-date source repos which you can update with `pod repo update` or with `pod install --repo-update`.
 * mistyped the name or version.
 * not added the source repo that hosts the Podspec to your Podfile.
  • 根据提示,执行pod install --repo-update,发现特别慢,并且中间会断;
hello:mpaasapp_ios zxs$ pod install --repo-update
Updating local specs repositories
  $ /usr/bin/git -C /Users/zxs/.cocoapods/repos/master fetch origin --progress
  remote: Enumerating objects: 123701, done.        
  remote: Counting objects: 100% (123701/123701), done.        
  remote: Compressing objects: 100% (114/114), done.        
  error: RPC failed; curl 18 transfer closed with outstanding read data remaining
  fatal: the remote end hung up unexpectedly
  fatal: early EOF
  fatal: index-pack failed
[!] CocoaPods was not able to update the `master` repo. If this is an unexpected issue and persists you can inspect it running `pod repo update --verbose`
  • 没办法,只能慢慢等。
  • 在3%;10%;20%;甚至有一次是91%的时候还断了;下次开始,又会重新开始,没有断点续传的功能
  • 下载本地库(Spec),更新本地库(Spec)是CocoaPod最让人恼火的缺陷之一;
  • 第一部分大小有433M以上,下载速度只有几十K,并且中间会断;
  • 以前这个问题也存在,只是现在更严重了;

手动下载本地库

  • 删除原有的

在本地有的话需要先删除,目录位置用户目录/.cocoapos/repos/,这是一个隐藏目录

image.png
  • 方法1:手动clone;直接从官网clone;

git clone https://github.com/CocoaPods/Specs.git ~/.cocoapods/repos/trunk

这个和用命令行pod install --repo-update的效果差不多;作用不大

  • 方法1:手动clone;从国内镜像clone

git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git ~/.cocoapods/repos/trunk

这个会快一些,快的时候有几M,但慢的时候也只有几K;相比而言用这个镜像效果要好多了;只是中间也是有可能断的,也需要多次尝试;
CocoaPods安装方法-2020.12.18

  • 方法3:直接去GitHub网站下载压缩包,然后在本地解压。
image.png

这个方法会快很多,也不用担心中间会断,更加稳定。下载之后,解压:

image.png

把解压后的文件夹移动到目标位置,并改名为trunk:

image.png
image.png

这样直接改文件名之后,执行pod setup, pod repo list等命令是没问题的。

hello:~ zxs$ pod repo list

trunk
- Type: CDN
- URL:  https://cdn.cocoapods.org/
- Path: /Users/zxs/.cocoapods/repos/trunk

1 repo
hello:~ zxs$ pod setup
Setup completed

但是,执行pod repo update命令就会出现错误:

image.png

这是什么原因?该怎么解决呢?

原因:下载的zip解压之后,光改名为trunk还不行,还要和git关联起来,里面要有git文件夹。下面这篇帖子,提到了类似的问题:
How to clone git repository from its zip

解决方案:既然少了git文件夹,那么就试着手动添加上去。
手动clone命令git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git ~/.cocoapods/repos/trunk会自动生成相应的git文件夹,copy出来放到这里就可以了。

image.png

添加了这个.git文件夹之后,pod repo update命令就不会出错了:

hello:~ zxs$ pod repo update
Updating spec repo `trunk`
hello:~ zxs$

下载第三方库

只要执行命令pod install就可以了,不过耗时过长

hello:mpaasapp_ios zxs$ pod install
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (4.0.1)
Installing IQKeyboardManager (6.5.6)
Installing MGSwipeTableCell (1.6.11)
Installing MJExtension (3.1.2)
Installing MJRefresh (3.5.0)
Installing TZImagePickerController (3.4.8)
Installing YYText (1.0.7)
Generating Pods project
Integrating client project
Pod installation complete! There are 7 dependencies from the Podfile and 7 total pods installed.
hello:mpaasapp_ios zxs$ 

如何加快速度呢?刚更新的本地库,就不需要update了。要检查本地库是否最新,然后更新,这个过程是很耗时的。所以,尝试一下如下命令:
解决CocoaPods慢的小技巧

pod install --no-repo-update --verbose
pod update --no-repo-update --verbose

这两个命令,添加了参数--no-repo-update,意思是:不升级本地库,可以节省一些时间。当然,如果真的遇到一个第三方库,下载很慢,那就没办法了(比如AliyunOSSiOS下载很慢,甚至下载失败)。
--verbose这个参数是打印中间过程log,比如这次AliyunOSSiOS这个第三方库下载遇到问题,到底是停在哪里了?这个参数可以给出更多的信息:

image.png
  • AliyunOSSiOS这个第三方库尝试了几次都不能成功下载,原因暂时还不知道。
    这个库在GitHub上是正常的:aliyun-oss-ios-sdk

这个库的作用是iOS客户端直接上传数据(比如头像)到阿里云,说实话这并不是很好的做法。这里有一篇文章说了这事:iOS版上传数据到阿里后台(AliyunOSSiOS)

另外,这个库非常冷门,工程中引入这样的第三方库是非常危险的:

image.png

没办法,AliyunOSSiOS这个第三方库就是下载不下来,只能先在Podfile中注释掉,过几天再试试。

查看第三方库

  • 下载之后,可以在工程目录的Pods文件夹看到第三方库的源文件:
image.png
  • 用Xcode打开工程名.xcworkspace文件,也可以看到已经下载的第三方库源文件:
image.png

多试几次

就剩下一个AliyunOSSiOS了,有空就多点几下,多试几次,说不定什么时候就好了。

hello:mpaasapp_ios zxs$ pod install --no-repo-update
Analyzing dependencies
Downloading dependencies
Installing AliyunOSSiOS (2.10.8)
Generating Pods project
Integrating client project
Pod installation complete! There are 8 dependencies from the Podfile and 8 total pods installed.
hello:mpaasapp_ios zxs$ 

这个就是单下AliyunOSSiOS的例子,多试几次,莫名其妙就好了。

小结

到这里基本上CocoaPods已经完成大部分工作了。个别第三方库,比如这里的AliyunOSSiOS不能下载的原因,就要另外百度找原因了。或者多试几次,突然就可以了,就像这次这样。

参考文章

pod install卡住得解决办法

移动开发平台 mPaaS

AFNetworking

MJExtension

aliyun-oss-ios-sdk

IQKeyboardManager

MJRefresh

MGSwipeTableCell

YYText

TZImagePickerController

你可能感兴趣的:(CocoaPods Spec手动下载实践-2020-12-30)