美文网首页iOS 技术分享iOS Developer
iOS-CocoaPods的安装及使用

iOS-CocoaPods的安装及使用

作者: Joh蜗牛 | 来源:发表于2017-03-29 11:19 被阅读25次

    一、概要

    iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库。

    二、安装

    由于网上的教程基本都大同小异,但细节之处还不是很完善,所以借机会在这里补充下:

    注:要使用CocoaPods,那就要下载安装它,而下载安装CocoaPods需要Ruby环境

    1、Ruby环境搭建

    当前安装环境为Mac mini 10.8.5。Mac  OS本身自带Ruby,但还是更新一下保险,因为我第一次安装在没有更新Ruby的情况下就失败了。

    a 查看下当前ruby版本:打开终端输入 ruby -v(确实安装了,不过用这个版本接下来工作失败了,所以更新下ruby)

    打开终端,输入 

     ruby -v  

    回车;

    b 更新ruby

    终端输入如下命令(把Ruby镜像指向taobao,避免被墙,你懂得)

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

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

    gem sources -l  (用来检查使用替换镜像位置成功)

    再输入

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

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

    gem sources -l

    回车;

    2、下载安装CocoaPods

    终端输入:sudo gem install cocoapods

    再输入

    sudo gem install cocoapods

    回车;

    Installing ri documentation for cocoapods-0.32.1

    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rdoc/rdoc.rb:280: warning: conflicting chdir during another chdir block

    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rdoc/rdoc.rb:287: warning: conflicting chdir during another chdir block

    Done installing documentation for cocoapods after 10 seconds

    1 gem installed

    这样就下载安装好了CocoaPods


    3、使用CocoaPods

    a  新建一个项目,名字PodTest

    b  终端中,cd到项目总目录(注意:包含PodTest文件夹、PodTest.xcodeproj、PodTestTest的那个总目录)

    c  建立Podfile(配置文件)

     cd 到项目总目录,输入

    vim Podfile

    回车;

    输入i,进入编辑模式,

    回车;

    输入

    platform :ios, '8.0'

    #use_frameworks!

    target '项目名称' do———————记得改名字

    pod 'AFNetworking', '~> 3.0’

    end

    然后按Esc,并且输入“:”号进入vim命令模式,然后在冒号后边输入wq


    cd到项目总目录pod install

    转载自:blog.csdn.net/showhilllee/article/details/38398119/

    今天尝试用 Cocoapods安装个第三方库.. 输入pod install, 发现 command not find。

    估计是升级10.11后Cocoapods被干掉了。

    我输入 sudo gem install cocoa pods 之后,出现如下问题:

    ERROR:  While executing gem ... (Gem::DependencyError)

    Unable to resolve dependencies: cocoapods requires cocoapods-core (= 0.33.1), claide (~> 0.6.1), cocoapods-downloader (~> 0.6.1), cocoapods-plugins (~> 0.2.0), cocoapods-try (~> 0.3.0), cocoapods-trunk (~> 0.1.1), nap (~> 0.7)

    解决方法:sudogem update --system

    但是出现了另一个错误:

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

    Operationnotpermitted - /usr/bin/xcodeproj

    在Stackoverflow上找到了解决方法:

    在终端中输入:sudo nvram boot-args="rootless=0"; sudo reboot

    然后你的电脑会重启

    之后再输入sudo gem install cocoapods -V

    就可以了

    不放心的话输入

    pod --version

    0.37.2 //显示出版本就说明成功了

    但是之后我pod install的时候又花式出错

    [!] Unable to add a source with url `https://github.com/CocoaPods/Specs.Git` named `master`.

    You can try adding it manually in `~/.cocoapods/repos` or via `pod repo add`.

    我尝试按提示的方法

    pod repo add master https://github.com/CocoaPods/Specs.git

    然而还是有错..

    [!] /usr/bin/git clone http://git.oschina.NET/akuandev/Specs.git master

    xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

    最后的解决方法:

    sudo xcode-select -switch /Applications/Xcode-beta.app/Contents/Developers

    后面的地址你可以打开Xcode显示包内容,找到那个文件夹拖到终端里面比较不容易错。

    CocoaPods 0.38.0.beta.2 is available.

    To update use: `gem install cocoapods --pre`

    [!] This is a test version we'd love you to try.

    For more information see http://blog.cocoapods.org

    and the CHANGELOG for this version http://git.io/BaH8pQ.

    CocoaPods 0.38.0.beta.2 is available.

    To update use: `gem install cocoapods --pre`

    [!] This is a test version we'd love you to try.

    For more information see http://blog.cocoapods.org

    and the CHANGELOG for this version http://git.io/BaH8pQ.

    最后终于修成正果..

    至于一开始输入 sudo gem installcocoapods 没反应需要修改为淘宝镜像的问题,请百度Cocoapods参阅其它文章..

    转载自:http://blog.csdn.net/NSNirvana/article/details/46873071

    1.设置ruby的软件源

    这是因为ruby的软件源rubygems.org因为使用亚马逊的云服务,被我天朝屏蔽了,需要更新一下ruby的源,过程如下:

    gem sources -l #(查看当前ruby的源)

    gem sources --remove https://rubygems.org/ #(移除当前ruby的源)

    gem sources -a https://ruby.taobao.org/ #(设置当前ruby的源为我天朝的)

    gem sources -l #(再次查看当前ruby的源)

    如果Terminal输出:

    *** CURRENT SOURCES ***

    https://ruby.taobao.org/

    就证明ruby的软件源已经设置OK了。

    2.设置gem为最新版本

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

    在Terminal输入以下命令:

    sudo gem update --system

    升级成功后会提示: Latest version currently installed. Aborting.

    3.执行安装CocoaPods命令

    注意:OS X 10.11 升级,虽然官方声称只是一个小的升级,但对于开发者而言,cocoapods需要重新安装,但按照以前的安装方式,在Terminal输入以下命令:

    sudo gem install  cocoapods

    如果报以下错误:

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

    Operation not permitted - /usr/bin/xcodeproj

    解决方案1:

    执行下面的命令并重启

    sudo nvram boot-args="rootless=0"

    sudo reboot

    重启之后, 执行这个命令检查

    sudo gem install cocoapods -V

    如果依旧有错误,使用第二个方案

    解决方案2:

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

    pod setup

    还有一点需要注意,pod setup在执行时,会输出Setting up CocoaPods master repo,但是会等待比较久的时间。这步其实是 Cocoapods 在将它的信息下载到 ~/.cocoapods目录下,如果你等太久,可以试着 cd 到那个目录,用du -sh *来查看下载进度。

    安装成功后,你会看到:Setup completed

    五、Cocoapods的使用

    随便以一种方式新建一个名为Podfile的文件放到你的工程根目录下(不能写成别的名字,也可以自己在工程根目录里面直接新建)

    Podfile文件内容的格式应该如下:

    platform :ios, '8.0' #(注明你的开发平台以及版本,'8.0'忽略不写即为最新版本)

    pod 'AFNetworking', '~> 2.5.3' #('~> 2.5.3'为版本号,忽略不写即为最新版本)

    pod 'SDWebImage', '~> 3.7.2'

    然后在Terminal进入工程所在的根目录(工程根目录)中执行 :

    pod install

    这样,AFNetworking和SDWebImage就已经下载完成并且设置好了编译参数和依赖,以后使用的时候切记如下两点:

    1.从此以后需要使用Cocoapods生成的 .xcworkspace文件来打开工程,而不是使用以前的.xcodeproj文件

    2.每次更改了Podfile文件,都需要重新执行一次pod update命令

    添加:

    错误1:

    Error fetchinghttp://ruby.taobao.org/:

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

    解决方案:把安装流程中 $gem sources -ahttp://ruby.taobao.org/---改为---->$gem sources -ahttps://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、设置 ruby 的软件源。

    由于默认的 ruby 的软件源(https://rubygems.org/)被墙阻拦导致 CocoaPods 安装失败,因此需要更新一下 ruby 的源。依次执行如下命令:

    gem sources -l #(查看当前ruby的源)

    gem sources --remove https://rubygems.org/ #(移除当前ruby的源)

    gem sources -a https://ruby.taobao.org/ #(设置当前ruby源为淘宝源)

    gem sources -l #(再次查看当前ruby的源)

    显示如下文字表示更新成功。

    *** CURRENT SOURCES ***

    https://ruby.taobao.org/

    2、升级 gem。

    gem 版本较低也可能导致安装或者升级失败,所以需要升级 gem。使用以下命令:

    sudo gem update --system

    3、安装 CocoaPods

    检查一下您的 OS X 的版本。

    若您的 OS X 版本小于等于 10.11,依次执行下面的命令安装 CocoaPods。

    sudo gem install cocoapods

    pod setup

    大于 10.11,使用如下命令安装。

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

    pod setup

    注意:安装过程可能会耗时比较长,也有可能受网络状况影响造成失败,需要多次尝试直到成功。

    相关文章

      网友评论

        本文标题:iOS-CocoaPods的安装及使用

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