一、什么是CocoaPods
CocoaPods是iOS项目的依赖管理工具,该项目源码在Github上管理。开发iOS项目不可避免地要使用第三方开源库,CocoaPods的出现使得我们可以节省设置和第三方开源库的时间。
4.管理他们的更新
使用CocoaPods
我们只需要把用到的开源库放到一个名为Podfile的文件中,然后执行pod install.Cocoapods就会自动将这些第三方开源库的源码下载下来,并且为我们的工程设置好响应的系统依赖和编译参数。
sudo gem install 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
sudo gem update --system
升级成功后会提示: RubyGems system software updated
sudo gem install cocoapods
pod setup
四、Cocoapods的使用
1、首先创建Podfile
在终端输入:jack 改为你用户名,注意文件目录和你的是否相符
cd /Users/Jack/Desktop/cocoaPods touch Podfile
2、编辑Podfile
文本打开podfile文件,如需要导入AFNetworking库,则输入 注意:版本号
platform :ios,’9.2’
pod ‘AFNetworking’,’~>2.0’
如图:
3、 执行导入命令
返回终端,开始导入第三方库,输入下列命令:
cd /Users/wangzz/Desktop/CocoaPodsTest 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.然后返回文件目录,看看是否多出文件,如图:
请关闭任何 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已经给出了,只是一开始没有好好看