美文网首页iOS开发技术分享
Mac上Cocoapods的安装和更新

Mac上Cocoapods的安装和更新

作者: 滑翔Skate | 来源:发表于2017-01-20 15:16 被阅读946次

    Mac上Cocoapods的安装和更新

    原CSDN博客:Mac上cocoapods的安装和更新转到简书

    前言

    记录下Mac上Cocoapods的更新,特别是升级到OS X EL Capitan后,出现了很多问题,主要是权限。在每次更新时都需要查阅资料,特此记录下。

    步骤

    1.gem更新源

    国内需切换 gem source
    老版本更新gem:

    $ sudo gem update --system
    

    OS X EL Capitan下会报错误:

    $ ruby -v
    ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin15]
    $ sudo gem update --system
    Password:
    Updating rubygems-update
    ERROR:  While executing gem ... (Errno::EPERM)
    Operation not permitted - /usr/bin/update_rubygems
    

    尝试:

    $ sudo gem update - /usr/bin/update_rubygems
    Updating installed gems
    Nothing to update
    

    表示不需要更新。
    切换gem source,并查看

    $ gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
    
    $ gem sources -l
    *** CURRENT SOURCES***
    
    https://ruby.taobao.org
    

    2.安装cocoapods

    老版本的系统下的安装:

    $sudo gem install cocoapods
    

    OS X EL Capitan下会报错误:

    $ sudo gem install cocoapods
    ERROR:  While executing gem ... (Errno::EPERM)
    Operation not permitted - /usr/bin/xcodeproj
    

    需要改成如下命令:

    $ sudo gem install -n /usr/local/bin cocoapods
    

    成功后提示:

    Successfully installed xcodeproj-1.2.0
    Fetching: fourflusher-0.3.2.gem (100%)
    Successfully installed fourflusher-0.3.2
    Fetching: cocoapods-1.0.1.gem (100%)
    Successfully installed cocoapods-1.0.1
    Parsing documentation for xcodeproj-1.2.0
    Installing ri documentation for xcodeproj-1.2.0
    Parsing documentation for fourflusher-0.3.2
    Installing ri documentation for fourflusher-0.3.2
    Parsing documentation for cocoapods-1.0.1
    Installing ri documentation for cocoapods-1.0.1
    3 gems installed
    

    最后

    $pod setup
    

    此过程中,有可能终端会卡在Setting up CocoaPods master repo不动,其实并不是真正的卡住,而是在下载安装第三方库的索引,可以参考这篇文章进行解决:
    使用CocoaPods卡在了"pod setup"界面的解决办法
    这里还可能遇到几个问题
    1.如下所示:

    [!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git master
    
    Cloning into 'master'...
    error: RPC failed; curl 56 SSLRead() return error -36
    fatal: The remote end hung up unexpectedly
    fatal: early EOF
    fatal: index-pack failed
    

    解决办法:
    (1)终端中输入如下命令:

    sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
    

    验证下:

    sudo xcode-select --print-path
    

    会输出:

    /Applications/Xcode.app/Contents/Developer
    

    (2)然后删除repos目录下master

    sudo rm -fr ~/.cocoapods/repos/master
    

    再执行pod setup。
    从github上下载repos会很慢,可以通过镜像渠道来提高速度。
    2.clone大小限制
    这个是用于在执行pod setup命令的时候,里面会执行从github上面clone资源到本地的语句,但是要clone的东西太大了,超过的git限制的大小.尝试执行以下语句把默认的限制变大:(52428000=500×1024×1024,即500M)

    git config http.postBuffer 524288000   
    (如果这里提示:fatal: not in a git directory, 可以加上--global : git config --global http.postBuffer 524288000)
    

    之前git中的配置是没有这一项的,执行完以上语句后输入git config -l可以看到配置项的最下面多出了一行我们刚刚配置的内容。最后,再次执行pod setup。

    3.更新CocoaPods

    #mac osx 10.11 之后
    $ sudo gem install -n /usr/local/bin cocoapods --pre //安装最新版本
    $ sudo gem install -n /usr/local/bin cocoapods -v <version> //安装指定的 version
    

    然后查看版本

    $ pod --version
    

    4.用cocoapods为项目配置第三方库

    可以通过 $pod$ 或 $pod --help$帮助命令进行配置:

    $pod
    $pod --help
    

    这里作个简介:
    (1) 新建工程,并在终端用cd指令到文件夹内

    $pod search 第三方
    

    (2)新建文件 vim “Podfile”,或者 pod init

    $vim Podfile
    

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

    platform:ios, '7.0'   
    pod 'AFNetworking', '~> 2.3.1'    <-------第三方
    

    (3)导入第三方库

    $pod install
    

    这一步可能在 updating local specsrepositories 卡住不动,原因是 pod install 或 update 被墙了,换成新的命令pod install(或update) --verbose --no-repo-update。
    如果遇到 WARNING: CocoaPods requires your terminal to be using UTF-8 encoding. 错误时,WARNING的意思是你的终端的编码不是UTF-8,需要改为UTF-8
    请在终端执行 export LC_ALL="en_US.UTF-8"或者 在偏好设置里设置编码。
    (4)退出终端

    参考

    [1] Cocoa pods的安装和使用详解
    [2] CocoaPods 安装 使用
    [3] 更新过程中遇到的rpc -36错误

    相关文章

      网友评论

      本文标题:Mac上Cocoapods的安装和更新

      本文链接:https://www.haomeiwen.com/subject/zanwbttx.html