最新版 CocoaPods 的安装流程

来源:互联网 发布:printthis.js用法 编辑:程序博客网 时间:2024/06/10 02:22

原文链接:http://www.jianshu.com/p/6e5c0f78200a
一、什么是CocoaPods
CocoaPods是iOS项目的依赖管理工具,该项目源码在Github上管理。开发iOS项目不可避免地要使用第三方开源库,CocoaPods的出现使得我们可以节省设置和第三方开源库的时间。

在使用CocoaPods之前,开发项目需要用到第三方开源库的时候,我们需要
1.把开源库的源代码复制到项目中
2.添加一些依赖框架和动态库
3.设置-ObjC,-fno-objc-arc等参数
4.管理他们的更新
在使用CocoaPods后,我们只需要把用到的开源库放到一个名为Podfile的文件中,然后执行pod install.Cocoapods就会自动将这些第三方开源库的源码下载下来,并且为我们的工程设置好响应的系统依赖和编译参数。

二、CocoaPods的原理

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



iOS 最新版 CocoaPods 的安装流程

1.移除现有Ruby默认源

$gem sources --remove https://rubygems.org/

2.使用新的源

$gem sources -a https://ruby.taobao.org/

3.验证新源是否替换成功

$gem sources -l

4.安装CocoaPods

(1) $sudo gem install cocoapods 备注:苹果系统升级 OS X EL Capitan 后改为$sudo gem install -n /usr/local/bin cocoapods

(2) $pod setup

5.更新gem

$sudo gem update --system

6. 新建工程,并在终端用cd指令到文件夹内

$pod search 第三方

7.新建文件 vim “Podfile”,

$vim Podfile

写入以下内容并保存 小提示:(终端vim文件 按 i 可编辑 ,esc 退出编辑,:wq  可保存退出)

platform:ios, '6.0'   

pod 'AFNetworking', '~> 2.3.1'    <-------第三方

8.导入第三方库

$pod install

9.退出终端

以下是我用以前的安装流程安装时出现的一些错误

终端  cocoapods 下载bug调试:

错误1:

Error fetching http://ruby.taobao.org/:

bad response Not Found 404 (http://ruby.taobao.org/specs.4.8.gz)

解决方案:把安装流程中 $gem sources -a http://ruby.taobao.org/   ---改为---->$gem sources -a https://ruby.taobao.org/

错误2:

ERROR:  While executing gem ... (Errno::EPERM)

Operation not permitted - /usr/bin/pod

解决方案:苹果系统升级OS X EL Capitan后会出现的插件错误,将安装流程 4.安装CocoaPods 的 (1)sudo gem install cocoapods ——>改为sudo gem install -n /usr/local/bin cocoapods

错误3:

[!] Unable to satisfy the following requirements: - `AVOSCloud (~> 3.1.6.3)` required by `Podfile`

Specs satisfying the `AVOSCloud (~> 3.1.6.3)` dependency were found, but they required a higher minimum deployment target.

解决方案:安装流程:Podfile文件 中   platform:ios, ‘6.0’  后边的 6.0 是平台版本号 ,一定要加上

常见问题

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命令查看,已经改过来了

  1. [!] 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_ROOTOther 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已经给出了,只是一开始没有好好看:


03.png


文/CoderAO(简书作者)
原文链接:http://www.jianshu.com/p/6e5c0f78200a
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。


0 0