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

再或者
The XX [Debug] target overrides the ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES build setting defined in `Pods/Target Support Files/Pods-XX/Pods-XX.debug.xcconfig'. This can lead to problems with the CocoaPods installation

解决方法:
在build setting 搜索ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES,选择该条目,按delete键。

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
11.Please contact the author or set the SWIFT_VERSION attribute in at least one of the targets that integrate this pod.
例如:
- `ChartsRealm` does not specify a Swift version and none of the targets (`Ates`) integrating it have the `SWIFT_VERSION` attribute set. Please contact the author or set the `SWIFT_VERSION` attribute in at least one of the targets that integrate this pod.
- `RealmSwift` does not specify a Swift version and none of the targets (`Ates`) integrating it have the `SWIFT_VERSION` attribute set. Please contact the author or set the `SWIFT_VERSION` attribute in at least one of the targets that integrate this pod.

解决办法,如果是纯OC工程,请尝试新建一个Swift类,生成桥接文件,然后,选中对应的target,--> build setting --> Swift Compiler Language --> Swift Language Version,选择一个Swift版本。

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容