iOS-CocoaPods的介绍、安装、使用、问题

一、什么是CocoaPods

CocoaPods是iOS项目的依赖管理工具,该项目源码在Github上管理。开发iOS项目不可避免地要使用第三方开源库,CocoaPods的出现使得我们可以节省设置和第三方开源库的时间。


平时我们开发项目需要用到第三方开源库的时候,我们需要
1.把开源库的源代码复制到项目中
2.添加一些依赖框架和动态库
3.设置-ObjC,-fno-objc-arc等参数

4.管理他们的更新


使用CocoaPods

我们只需要把用到的开源库放到一个名为Podfile的文件中,然后执行pod install.Cocoapods就会自动将这些第三方开源库的源码下载下来,并且为我们的工程设置好响应的系统依赖和编译参数。



二、CocoaPods的原理
CocoaPods的原理是将所有的依赖库都放到另一个名为Pods的项目中,然后让主项目依赖Pods项目,这样,源码管理工作都从主项目移到了Pods项目中。Pods项目最终会编译成一个名为libPods.a的文件,主项目只需要依赖这个.a文件即可。


三、CocoaPods的安装
CocoaPods可以方便地通过Mac自带的RubyGems安装。
(1)打开终端Terminal,然后键入以下命令: 
sudo gem install cocoapods


执行完这句如果报告以下错误:
ERROR: Could not find a valid gem 'cocoapods' (>= 0), here is why:
Unable to download data from https://rubygems.org/ - Errno::ETIMEDOUT: Operation timed out - connect(2) (https://rubygems.org/latest_specs.4.8.gz)
ERROR: Possible alternatives: cocoapods


这是因为ruby的软件源rubygems.org因为使用亚马逊的云服务,被我大天朝屏蔽了

(2)需要更新一下ruby的源,逐一输入如下代码:

gem sources -l                
gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
gem sources -l




如果gem太老,可以尝试用如下命令升级gem

 sudo gem update --system

升级成功后会提示: RubyGems system software updated




(3)然后重新执行安装下载命令
sudo gem install cocoapods

这时候应该没什么问题了


(4)接下来进行安装,执行:
pod setup



终端Terminal会停留在Setting up CocoaPods master repo 这个状态一段时间,是因为要进行下载安装,而且目录比较大,需要耐心等待一下.如果想加快速度,可使用cocoapods的镜像索引.(文章末尾附使用镜像索引的方法)
安装成功后,你会看到:



四、Cocoapods的使用


1、首先创建Podfile
在终端输入:jack 改为你用户名,注意文件目录和你的是否相符

cd /Users/Jack/Desktop/cocoaPods
touch Podfile


进入工程的文件,你会发现空白的Podfile文件,如下图:
iOS-CocoaPods的介绍、安装、使用、问题_第1张图片

2、编辑Podfile

文本打开podfile文件,如需要导入AFNetworking库,则输入 注意:版本号

platform :ios,’9.2’
pod ‘AFNetworking’,’~>2.0’

如图:

3、 执行导入命令

返回终端,开始导入第三方库,输入下列命令:

cd /Users/wangzz/Desktop/CocoaPodsTest  
pod install  

上述命令为进入工程根目录,然后执行pod install命令

CocoaPods就开始为我们做下载源码、配置依赖关系、引入需要的framework等一些列工作,命令的执行结果打印出来如下:

(大体为这样,并不相同)

Analyzing dependencies

Downloading dependencies

Installing AFNetworking (2.6.3)

Generating Pods project

Integrating client project

[!] From now on use `CocoaPodsTest.xcworkspace`.  

4.然后返回文件目录,看看是否多出文件,如图:

iOS-CocoaPods的介绍、安装、使用、问题_第2张图片

请关闭任何 Xcode 会话 cmd +Q,重新打开此项目使用 'cocoaPods.xcworkspace'即可


5.打开工程后只需在你要用的文件,引用头文件即可 

如:#import <AFNetworking.h>
如图:




五、常见问题
1.
[!] Invalid Podfile file: undefined local variable or method `en_US' for #<Pod::Podfile:0x00000102a5d8b0>. Updating CocoaPods might fix the issue.


原因:单引号格式,可能是手动输入导致
解决办法:系统偏好设置-键盘-文本-将“使用智能引号和破折号”一项取消勾选-再将podfile里面的单(双)引号修改一下


2.ArgumentError - invalid byte sequence in US-ASCII
原因:字符集错误
解决办法:
使用locale命令查看当前的字符集,如果都是zh,需要执行以下命令:
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
然后再使用locale命令查看,已经改过来了


[!] The YMTea [Debug] target overrides the OTHER_LDFLAGS build setting defined in `Pods/Target Support Files/Pods/Pods.debug.xcconfig'. This can lead to problems with the CocoaPods installation
Use the $(inherited) flag, or
Remove the build settings from the target.
[!] The YMTea [Release] target overrides the OTHER_LDFLAGS build setting defined in `Pods/Target Support Files/Pods/Pods.release.xcconfig'. This can lead to problems with the CocoaPods installation


- Use the `$(inherited)` flag, or
- Remove the build settings from the target.
原因:我是在已有项目中集成Cocoapods的时候遇到这个问题,原因是项目 Target 中做了一些设置,CocoaPods 也做了默认的设置,如果两个设置结果不一致,就会造成问题。
解决方法:我想要使用 CocoaPods 中的设置,分别在我的项目中定义PODS_ROOT 和 Other Linker Flags的地方(build settings),把他们的值用$(inherited)替换掉,进入终端,执行 pod update
警告没了,回到 Xcode,build通过。
网上还流行另外一种简单粗暴的方法:点击项目文件 project.xcodeproj,右键显示包内容,用文本编辑器打开project.pbxproj,删除OTHER_LDFLAGS的地方,保存(这种我没试过)


4.
[!] Oh no, an error occurred.


It appears to have originated from your Podfile at line 2.


Search for existing GitHub issues similar to yours:
https://github.com/CocoaPods/CocoaPods/search?q=%2FUsers%2Fxiao6%2FMusic%2FGI06%E5%AE%9E%E8%AE%AD%E8%8A%B8%E8%8C%97%E8%8C%B6%E5%8F%B6%2FYMTea%2FPodfile%3A2%3A+syntax+error%2C+unexpected+%27%3A%27%2C+expecting+end-of-input%0Aplatform+%3A+ios%2C+%277.0%27%0A++++++++++%5E&type=Issues


If none exists, create a ticket, with the template displayed above, on:
https://github.com/CocoaPods/CocoaPods/issues/new


Be sure to first read the contributing guide for details on how to properly submit a ticket:
https://github.com/CocoaPods/CocoaPods/blob/master/CONTRIBUTING.md


Don't forget to anonymize any private data!


原因:这个问题比较蛋疼,弄了好久,仔细看发现就是因为Podfile文件里面 platform 那一行 冒号和ios之间多了一个空格。。。。其实这个错误在报错的时候ruby已经给出了,只是一开始没有好好看


你可能感兴趣的:(iOS-CocoaPods的介绍、安装、使用、问题)