Cocoapods

  • CocoaPods 是 iOS 最常用最有名的类库管理工具
  • 作为 iOS 程序员,掌握 CocoaPods 的使用是必不可少的基本技能

安装

# 删除源(这个系统自带的不好用)
$ sudo gem sources -r https://rubygems.org/

# 添加源(使用淘宝的镜像,记住要用https)
//$ sudo gem sources -a https://ruby.taobao.org/ 已弃用
$ gem sources -a  https://gems.ruby-china.org/

# 查看是否使用的是淘宝镜像
$ gem sources -l

# 安装
$ sudo gem install cocoapods(建议这个,更新内容比较详细)
$ sudo gem install cocoa pods
$ sudo gem install -n /usr/local/bin cocoa pods

# 安装成功之后,查看是否是最后版本
$ pod --version

# 配置pod环境
$ pod setup

注:如果发现安装不成功,这个时候,可以先删除淘宝的镜像再次添加一下

# 删除源(使用淘宝的镜像,记住要用https)
$ sudo gem sources -r https://ruby.taobao.org/

# 添加源(使用淘宝的镜像,记住要用https)
//$ sudo gem sources -a https://ruby.taobao.org
$ gem sources -a  https://gems.ruby-china.org/

# 如果安装失败 ~/.cocoapods 里面是空的, 就需要重新setup
$ pod repo remove master
$ pod setup

# 后续更新CocoaPods版本使用
$ sudo gem install cocoapods --pre

# 删除pod
$ sudo gem uninstall cocoa pods

使用

# 1.搜索。
# 先搜索一下,你需要用到的框架,看是否支持Cocoapods,
# 如果存在才能使用Cocoapods的方式去集成,如果不存在,
# 则只能直接拖入工程的方式
$ pod search AFNetworking

# 2.在项目根目录下 生成方式一个关键文件 Podfile
# (记住要在项目根目录下,并且没有后缀)
$ pod init

# 3.配置刚刚生成的Podfile文件
# 里面主要是打开平台,添加需要的第三方框架名称

# 4.安装
$ pod update

# 5.本地库安装 节省时间
$ pod install --no-repo-update

注意

如果不想在pod install ,pod update的时候升级repo库 ,
可以忽略掉,后面加上--no-repo-update, 但是这样会有个问题, 
如果github上pods的三方库有新版本了, 你本地搜索到的还是旧版本,
如果用的新版本号是无法装配的, 
所以每隔一段时间最好执行一下pod repo update, --verbose的意思是打印详细信息.

出现Setting up CocoaPods master repo, 
cd 到~/.cocoapods,如果repos文件夹大小一直没有变化, 因为那堵墙阻挡了cocoapods.org.

oschina是国内的服务器, 可以用它们CocoaPods索引库的镜像:

pod repo remove master
pod repo add master http://git.oschina.net/akuandev/Specs.git
pod repo update

第二条命令执行的时候会比较耗时,要去把整个specs仓库clone一下,
下载到 ~/.cocoapods里, cd 到~/.cocoapods, 
用du -sh *命令来查看文件大小,需要等待一段时间,
这个目录最终大小是几百M,每个CocoaPods版本不同.

删除三方库

在Xcode中修改Podfile文件, 删除想要删除的三方库, 然后终端执行pod install或pod update, 等待完成.

删除一些三方库后编译可能会报错: library not found for -l库名,
# 解决:
在Build Settings中搜索Other Linker Flags, 去掉-l"库名".
(有时不管用,应该是cocoapods的版本问题)

安装过程中常见的问题

[!] Pod::Executable clone 'https://github.com/CocoaPods/Specs.git' master

xcrun: error: active developer path ("/Users/xiakejie/工具/Xcode 2.app/Contents/Developer") does not exist,
 use xcode-select to change

解决上面这个问题, 使用下面这个命令:
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

使用pod search 搜索出来的第三框架版本过低解决办法

有时候在终端输入pod search AFN的时候,发现搜索出来的AFN版本比Github的版本低

这个时候在终端中输入 pod repo update 即可!,这个命令是去更新cocoapods的主库

其他错误

问题描述:

  • github下载的Demo,很多时候使用到CocoaPods,有的时候因为依赖关系或者版本问题不能编译运行。出现例如The sandbox is not sync with the Podfile.lock

解决方案

  • cd 到工程目录
  • 关闭当前的工作空间,删除掉文件夹中的workspace,然后重新pod install,install完成之后,通过workspace打开工作空间,clean+build即可
$ rm -rf XXXXXX.xcworkspace 
$ pod install

cocapods 更新第三方库遇到的问题

  • 卡在Updating local specs repositories

    • pod install 被墙了,换成pod install --verbose --no-repo-update
  • 出现这种警告

    • Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.

    • 不要使用文本编辑去编辑Podfile,使用Xcode编辑,或者使用终端敲命令去编辑。或者输入格式错误,没输入运行版本:$platform:ios, '9.0'

  • 使用cocoapods导入第三方类库后头文件没有代码提示

    • 选择Target -> Build Settings 菜单,找到"User Header Search Paths"设置项,新增一个值"${SRCROOT}",并且选择"Recursive"
  • Re-creating CocoaPods due to major version update.

    • 可以通过 $ pod --version 查看版本号;原因是podfile升级到最新版本,pod里的内容必须明确指出所用第三方库的target;可以修改Podfile文件的配置文件,让它兼容不指定固定版本;且又不报错;
    platform :ios, '7.0'
    
    target 'target_name’ do
    
    pod 'ReactiveCocoa', '~> 2.5'
    pod 'SDWebImage', '~> 3.7.5'
    pod 'AFNetworking', '~> 2.5.4'
    pod 'Masonry', '~> 0.6.4'
    pod 'JSONModel', '~> 1.2.0'
    pod 'JCAlertView', '~> 1.1.5'
    pod 'RongCloudIMKit', '2.5.2' #不要更新此库 源代码已被修改
    pod 'SMPageControl', '~> 1.2'
    pod 'FMDB'
    pod 'GTSDK'
    pod 'BaiduMapKit' #百度地图SDK
    pod 'UMengAnalytics-NO-IDFA'#友盟统计无IDFA版SDK
    pod 'Fabric'
    pod 'Crashlytics'
    pod 'CocoaLumberjack'
    pod 'TTTAttributedLabel'
    pod 'FLEX'
    pod 'GVUserDefaults'
    pod 'UMengFeedback', '~> 2.3.4'
    pod 'TYAttributedLabel', '~> 2.5.6’
    pod 'IQKeyboardManager'
    pod 'MWPhotoBrowser'
    
    end
    
    • 其中target_name要换成当前target名,最后不要忘记增加一个end结尾,当然如果你的工程是有多个target都要引用;则要使用link_with(这个一定要放在文件第一行)如下:
    link_with ['target_1' ,'target_2', 'target_3']
    platform :ios, '7.0'pod 'ReactiveCocoa', '~> 2.5'
    pod 'SDWebImage', '~> 3.7.5'
    pod 'AFNetworking', '~> 2.5.4'
    pod 'Masonry', '~> 0.6.4'
    pod 'JSONModel', '~> 1.2.0'
    pod 'JCAlertView', '~> 1.1.5'
    pod 'RongCloudIMKit', '2.5.2' #不要更新此库 源代码已被修改
    pod 'SMPageControl', '~> 1.2'
    pod 'FMDB'
    pod 'GTSDK'
    pod 'BaiduMapKit' #百度地图SDK
    pod 'UMengAnalytics-NO-IDFA'#友盟统计无IDFA版SDK
    pod 'Fabric'
    pod 'Crashlytics'
    pod 'CocoaLumberjack'
    pod 'TTTAttributedLabel'
    pod 'FLEX'
    pod 'GVUserDefaults'
    pod 'UMengFeedback', '~> 2.3.4'
    pod 'TYAttributedLabel', '~> 2.5.6’
    pod 'IQKeyboardManager'
    pod 'MWPhotoBrowser'
    
    • 不同的target引用不同的第三方插件
    target :'target_1' do
    platform :ios  
    pod 'Reachability',  '~> 3.0.0'  
    pod 'SBJson', '~> 4.0.0'  
    
    platform :ios, '7.0'  
    pod 'AFNetworking', '~> 2.0'
    end
    
    target :'target_2' do
    pod 'OpenUDID', '~> 1.0.0'
    end
    

CocoaPods: pod search 搜索类库失败的解决办法

  • 错误:[!] Unable to find a pod with name, author, summary, or descriptionmatching '······'

  • 解决:

    • pod setup成功后,依然不能pod search,是因为之前你执行pod search生成了search_index.json,此时需要删掉

    • 终端输入:rm ~/Library/Caches/CocoaPods/search_index.json

    • 删除成功后,再执行pod search

    • 输出:Creating search index for spec repo 'master'.. Done!

git clone时出现error: RPC failed; curl 18 transfer closed&nbs

git clone 出现的错误:
remote: Counting objects: 1111, done.
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

原因:git 库源文件过多,修改下默认值即可。curl的postBuffer 
默认值较小,配置下个这个值

配置如下:
$ git  config --global http.postBuffer 24288000
$ git config --list

results matching ""

    No results matching ""