CocoaPods工作原理及使用中遇到的问题

项目上线,终于又有时间研究些三方的东西了,在自己钻牛角的时候,不觉间陷入了误区。在项目实践过程中,跟老大也学到了很多,也感觉自己肚子有了些油水,可看到GitHub上那些大牛的开源大作,不免自卑。虽入行一年,承蒙老大看得起,交给了个带徒弟的任务,交流中受虐与进步同行,苦涩与暖心并进。虽然自己师范出身,可离合格的老师,真心还是有差距的,至少队友给的反馈是:“表达能力太差,永远活在自己的思路里”。突然想到了小岳岳一句话:“虽然每天工作很累,但是我挣得少啊!”,我要说的是:“你每天那么努力,忍受了那么多寂寞和痛苦,可我也没见你多么优秀啊!”,哥懂的还是太少。好了,下面是正题:

在项目引入第三方SDK过程中,各种引入库文件、各种改配置,特别是更新SDK的时候,着实是件让人头大的事情。常在网上看到别人使用自动化三方库管理工具Cocoapods,原项目中一直没敢尝试,最近一直在研究,使用中确有眼前一亮的感觉,不过中间也遇到了许多问题,卖弄如下:

安装流程步骤此处就不多讲了,网上随便搜下......


  • 在使用pod 命令引入文件,在终端中会提示:
    Analyzing dependencies
    Downloading dependencies
    Installing......
    Generating Pods project
    [!] From now on use ProjectName.xcworkspace.

如果使用CocoaPods组织管理三方库之后,就只能通过ProjectName.xcworkspace打开,如果打开原来的工程会编译报错。
执行pod命令之后,项目中会生成:ProjectName.xcworkspace、Podfile.lock、Pods等文件;

 


CocoaPods工作原理:

CocoaPods的工作主要是通过ProjectName.xcworkspace来组织的,在打开ProjectName.xcworkspace文件后,发现Xcode会多出一个Pods工程。

  1. 库文件引入及配置:
    库文件的引入主要由Pods工程中的Pods-ProjectName-frameworks.sh脚本负责,在每次编译的时候,该脚本会帮你把预引入的所有三方库文件打包的成ProjectName.a静态库文件,放在我们原Xcode工程中Framework文件夹下,供工程使用。
    如果Podfile使用了use_frameworks!,这是生成的是.framework的动态库文件。引入方式也略有不同。

  2. Resource文件:
    Resource资源文件主要由Pods工程中的Pods-ProjectName-resources.sh脚本负责,在每次编译的时候,该脚本会帮你将所有三方库的Resource文件copy到目标目录中。

  3. 依赖参数设置:
    在Pods工程中的的每个库文件都有一个相应的SDKName.xcconfig,在编译时,CocoaPods就是通过这些文件来设置所有的依赖参数的,编译后,在主工程的Pods文件夹下会生成两个配置文件,Pods-ProjectName.debug.xcconfigPods-ProjectName.release.xcconfig

使用中遇到的问题:


1. install和update命令的配置速度问题

在我们输入pod install或者pod update之后,CocoaPods首先会去匹配本地的spec库,在确认spec版本库不需要更新之后,才会下载相应的库文件,这样比较耗时,有时候,以为是卡死了呢。所以一般使用下面两个命令,跳过spec版本库更新匹配。

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

有朋友说不加--verbose,其实加--verbose的意义在于可以输出更详细的配置过程 debug信息,在书写时位置也可以换,跳过spec版本库更新匹配的重点是--no-repo-update,比如:

pod install --no-repo-update --verbose // 不更新,并打印出详细过程信息

2. The dependency **** is not used in any concrete target.

[!] The dependency UMengAnalytics-NO-IDFA is not used in any concrete target.
这个提示是因为,cocoapods升级为1.0以后,Podfile文件书写格式的问题,
1.0之前:

   platform :ios
   pod 'UMengAnalytics-NO-IDFA’
   pod 'MBProgressHUD', '~> 0.9.2'
   pod 'FMDB'
   pod 'SDWebImage', '~> 3.7.3'
   pod 'IQKeyboardManager', '~> 3.2.4'
   pod 'MJRefresh', '~> 2.3.2'
   pod 'MJExtension', '~> 0.2.0'

1.0之后:

   platform :ios,’7.0’
   target ‘ProjectName’ do #ProjectName工程名字
   pod 'MBProgressHUD', '~> 0.9.2'
   pod 'FMDB'
   pod 'SDWebImage', '~> 3.7.3'
   pod 'IQKeyboardManager', '~> 3.2.4'
   pod 'MJRefresh', '~> 2.3.2'
   pod 'MJExtension', '~> 0.2.0'
   pod 'UMengAnalytics-NO-IDFA’
   end

✨波浪线**~ > ** 含义:从指定版本到倒数第二位版本号升1为止,比如 ‘~> 0.3.7’所指的版本区间为[0.3.7, 0.4.0),即>=版本0.3.7,<版本0.4.0,详见guides.cocoapods.org

3. Unable to satisfy the following requirements: - *** required by Podfile

这种提示主要是因为要添加的类库有最新版本,而你本地local specs repositories并没有更新其下载版本导致。
比如 Unable to satisfy the following requirements: - SDWebImage (~> 3.8) required by Podfile
处理方式有两种:
1、pod update更新本地库
2、降低Podfile文件中的版本;

4. 使用CocoaPods之后,头文件无法自动补齐问题

使用CocoaPods来管理三方库,还是比较方便的,但是突然发现一个美中不足的小问题,在使用import引入文件时,不能自动补齐,需要手工copy文件名,纠结了半天:
解决办法:
Target -> Build Settings ,User Header Search Paths条目中,添加${SRCROOT}或者$(PODS_ROOT),并且选择Recursive,递归搜索,然后就可以自动补齐了。

5. 在项目中移除CocoaPods三方库配置文件

如果我们在配置CocoaPods的三方库文件后,不在需要了可以移除指定库文件配置,具体步骤如下:

  • 删除工程文件夹下的Podfile、Podfile.lock和Pods文件夹;

  • 删除xcworkspace文件;

  • 打开xcodeproj文件,删除项目中的libpods.a和Pods.xcconfig引用;

  • 打开Build Phases选项,删除Check Pods Manifest.lock和Copy Pods Resources;

  • 重新pod install

  • 如果不想使用pod了,可以使用 pod deintegrate,移除三方库,手动添加。

6. Pods written in Swift can only be integrated as frameworks; add use_frameworks! to your Podfile or target to opt into using it.

这种提示,主要是因为要添加的这个库有专有swift库,或者demo中有swift代码。
解决办法:在Podfile文件的target后面添加use_frameworks!,注意,这里有!。
比如[!] Pods written in Swift can only be integrated as frameworks; add `use_frameworks!` to your Podfile or target to opt into using it. The Swift Pods being used are: ReactiveCocoa, ReactiveSwift, and Result

7. The XXXX [Debug] target overrides the HEADER_SEARCH_PATHS build setting defined in `Pods/Target Support Files/Pods-XXXX/Pods-XXXX.debug.xcconfig'. This can lead to problems with the CocoaPods installation

或者
The XXXX [Debug] target overrides the OTHER_LDFLAGS build setting defined in `Pods/Target Support Files/Pods-XXXX/Pods-XXXX.debug.xcconfig'. This can lead to problems with the CocoaPods installation
参考:the-target-overrides
解决方法:

  • Go to your target Build Settings -> Other linker flags -> double click . Add $(inherited) to a new line.

  • All these 3 errors would be gone by adding $(inherited) to

Header Search Paths
Other Linker Flags
Preprocessor Macros
in Project -> Target -> Build Settings
重新pod
pod install

8. Xcode10版本工程cocoapod <=1.5.3问题

RuntimeError - [!] Xcodeproj doesn't know about the following attributes {"inputFileListPaths"=>[], "outputFileListPaths"=>[]} for the 'PBXShellScriptBuildPhase' isa.

RuntimeError - [!] Xcodeproj doesn't know about the following attributes {"inputFileListPaths"=>[], "outputFileListPaths"=>[]} for the 'PBXShellScriptBuildPhase' isa.

参考:RuntimeError - [!] Xcodeproj doesn't know about the following
解决办法:
This is a known bug introduced by Xcode 10 which fixed in CocoaPods 1.6.0. Right now (Sep 2018) it's in beta, so you can install it with

sudo gem install cocoapods --pre
或者
sudo gem install -n /usr/local/bin cocoapods --pre

Alternative solution for CocoaPods 1.5.3 could be found here.

9. Errno::EPERM - Operation not permitted @ chmod_internal

Errno::EPERM - Operation not permitted @ chmod_internal - /Users/admin/Desktop/SoamBaMeng/Pods/AFNetworking/AFNetworking/AFHTTPSessionManager.m
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/fileutils.rb:1346:in `chmod'
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/fileutils.rb:1346:in `chmod'
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/fileutils.rb:1001:in `block in chmod'
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/fileutils.rb:1000:in `each'
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/fileutils.rb:1000:in `chmod'

这个问题为文件访问权限问题
解决办法:

移除Pods文件夹,重新pod install

10. error: RPC failed fatal: early EOF

......
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

解决办法

admin:~ admin$ git config --global http.postBuffer 24288000
admin:~ admin$ git config --list
credential.helper=osxkeychain
http.postbuffer=24288000

本文已在版权印备案,如需转载请在版权印获取授权。
获取版权

给俺GitHub来个Star:https://github.com/zonghongyan



作者:仁伯
链接:https://www.jianshu.com/p/f089fae248c1
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值