美文网首页
CocoaPods的安装和常见问题

CocoaPods的安装和常见问题

作者: RadioWaves | 来源:发表于2017-07-06 15:29 被阅读65次

    一、什么是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文件即可。

    三、CocoaPods的安装

    • CocoaPods可以方便地通过Mac自带的RubyGems安装。打开Terminal,然后键入以下命令:
    $ sudo gem install cocoapods
    
    • 执行完这句如果报告以下错误:
    ERROR: Could not find a valid gem 'cocoapods' (>= 0), 
    here is why:Unable to download data from [https://rubygems.org/]
    (https://rubygems.org/) - Errno::ETIMEDOUT: Operation timed out - connect(2) 
    ([https://rubygems.org/latest_specs.4.8.gz]
    (https://rubygems.org/latest_specs.4.8.gz))ERROR: Possible alternatives: cocoapods
    
    • 这是因为ruby的软件源rubygems.org因为使用亚马逊的云服务,被我天朝屏蔽了,需要更新一下ruby的源,过程如下:(以前使用的是taobao.org,但是现在已经不跟新,我们来用ruby中国的源,)
    // 先删除当前所有的ruby源,然后替换成
    $ gem sources -l (查看当前ruby的源)
    $ gem sources --remove (https://rubygems.org/)
    $ gem sources --remove (https://ruby.taobao.org/ )
    $ gem sources -a (https://gems.ruby-china.org/)
    $ gem sources -l
    
    • 如果gem太老,可以尝试用如下命令升级
    sudo gem update --system
    

    升级成功后会提示:

    RubyGems system software updated
    

    然后重新执行安装下载命令

    $ sudo gem install cocoapods
    

    如果在安装过程中出现:下图问题



    那就换个安装方法

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

    这时候应该没什么问题了,接下来进行安装,执行:

    $ pod setup
    

    Terminal会停留在 Setting up CocoaPods master repo 这个状态一段时间,是因为要进行下载安装,而且目录比较大,需要耐心等待一下.如果想加快速度,可使用cocoapods的镜像索引.(文章末尾附使用镜像索引的方法)安装成功后,你会看到:


    安装失败的解决方案

    sudo gem update --system
    sudo gem uninstall cocoapods
    sudo gem install cocoapods
    

    升级到10.11, CocoaPods报错: command not found 解决方案

    sudo gem update --system
    sudo gem uninstall cocoapods
    sudo gem install -n /usr/local/bin cocoa pods
    

    注意在Xcode 7以后,要将cocoapods的GEM_PATH更换为下图


    四、常见问题

    • 1 .
    [!] Invalid Podfile file: undefined local variable or method `en_US' for #
    <Pod::Podfile:0x00000102a5d8b0>. Updating CocoaPods might fix the issue.
    

    原因:单引号格式,可能是手动输入导致解决办法:系统偏好设置-键盘-文本-将“使用智能引号和破折号”一项取消勾选-再将podfile里面的单(双)引号修改一下

    ArgumentError - invalid byte sequence in US-ASCII
    

    原因:字符集错误解决办法:使用locale命令查看当前的字符集,如果都是zh,需要执行以下命令:

    export LC_ALL=en_US.UTF-8export 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 installationUse 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的地方,保存(这种我没试过)

     [!] 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%E
    8%8C%97%E8%8C%B6%E5%8F%B6%2FYMTea%2FPodfile%3A2%3A+syntax+error%2
    C+unexpected+%27%3A%27%2C+expecting+end-of-
    input%0Aplatform+%3A+ios%2C+%277.0%27%0A++++++++++%5E&type=Issues]
    (https://github.com/CocoaPods/CocoaPods/search?
    q=%2FUsers%2Fxiao6%2FMusic%2FGI06%E5%AE%9E%E8%AE%AD%E8%8A%B8%E
    8%8C%97%E8%8C%B6%E5%8F%B6%2FYMTea%2FPodfile%3A2%3A+syntax+error%2
    C+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]
    (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]
    (https://github.com/CocoaPods/CocoaPods/blob/master/CONTRIBUTING.md)
    Don't forget to anonymize any private data!
    

    原因:因为Podfile文件里面 platform 那一行 冒号和ios之间多了一个空格。。。。其实这个错误在报错的时候ruby已经给出了:


    附:如何使用CocoaPods的镜像索引:

    • 所有项目的Podspec文件都托管在https://github.com/CocoaPods/Specs
    • 第一次执行pod setup时,CocoaPods会将这些podspec索引文件更新到本地的~/.cocoapods目录下,这个索引文件比较大,所以第一次更新时非常慢.友好人士在国内的服务器建立了Cocoapods索引库的镜像,所以执行索引跟新操作时候会快很多.
      具体操作方法如下:
    $ pod repo remove master
    $ pod repo add master https://gitcafe.com/akuandev/Specs.git
    $ pod repo update
    这是使用gitcafe上的镜像,将以上代码中的 
    https://gitcafe.com/akuandev/Specs.git 
    替换成 
    http://git.oschina.net/akuandev/Specs.git 
    即可使用oschina上的镜像。
    

    以上内容主要来源于http://www.jianshu.com/p/4291014e01d1

    相关文章

      网友评论

          本文标题:CocoaPods的安装和常见问题

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