zoukankan      html  css  js  c++  java
  • mac10.12的Cocopods安装使用

    Cocopods的安装

    CocoaPods应该是iOS最常用最有名的类库管理
    当我们开发iOS应用时,会经常使用到很多第三方开源类库,比如AFNetWorking等等,可能某个类库又用到其他的库,手动一个个去下载所需类库十分麻烦。如果我们项目中用到的类库有更新,还要重新下载新版本,重新加入到项目中,十分麻烦。所以,我们需要 CocoaPods帮我们管理。
    如何下载和安装CocoaPods?
    在安装CocoaPods之前,首先要在本地安装好Ruby环境。至于如何在Mac中安装好Ruby环境,请找度娘和谷哥。
    假如你在本地已经安装好Ruby环境,那么下载和安装CocoaPods将十分简单,只需要一行命令。在Terminator中输入以下命令:

             sudo gem install cocoapods

    但是,在终端中敲入这个命令之后,可能半天没有任何反应。

    这时我们可以用淘宝的Ruby镜像来访问cocoapods。按照下面的顺序在终端中敲入依次敲入命令:

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

    //等有反应之后再敲入以下命令

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

    为了验证你的Ruby镜像是并且仅是taobao,可以用以下命令查看:

           $ gem sources -l

    只有在终端中出现下面文字才表明你上面的命令是成功的:

          *** CURRENT SOURCES ***
          https://ruby.taobao.org/

    这时候,我们再次在终端中运行:

            $ sudo gem install cocoapods
    
            $ pod setup

    等一会,CocoaPods就可以在你本地下载并且安装好了

    安装时候遇到的问题:
    Setting up CocoaPods master repo
    [!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git  master --depth=1
    
    Cloning into 'master'...
    error: RPC failed; result=18, HTTP code = 200
    fatal: The remote end hung up unexpectedly
    fatal: early EOF
    fatal: index-pack failed
    解决办法:
    $ git config --global http.postBuffer 24288000
    $ git config --list

    会输出:http.postbuffer=24288000

    再次执行:

    $ pod setup

    Cocopods使用

    我们想用CocoaPods,在项目中导入ASIHTTPRequest类库
    首先,我们要确定ASIHTTPRequest是否支持CocoaPods,可以用CocoaPods的搜索功能验证一下。在终端中输入:

           $ pod search ASIHTTPRequest

    输入之后,你会在终端中看到关于ASIHTTPRequest类库的一些信息。
    比如:

           -> ASIHTTPRequest (1.8.2)
              Easy to use CFNetwork wrapper for HTTP requests, Objective-C, Mac    OS X and  iPhone.
              pod 'ASIHTTPRequest', '~> 1.8.2'
              - Homepage: http://allseeing-i.com/ASIHTTPRequest
              - Source:   https://github.com/pokeb/asi-http-request.git
              - Versions: 1.8.2, 1.8.1 [master repo]
              - Sub specs:
              - ASIHTTPRequest/Core (1.8.2)
              - ASIHTTPRequest/ASIWebPageRequest (1.8.2)
              - ASIHTTPRequest/CloudFiles (1.8.2)
              - ASIHTTPRequest/S3 (1.8.2)

    现在知道ASIHTTPRequest支持Cocopods了,我们就要道你的项目中创建一个文件,这个文件就叫做“Podfile”,一定得是这个文件名,而且没有后缀。在里面添加pod 'ASIHTTPRequest', '~> 1.8.2',就是告诉CocoaPods,我们要下载ASIHTTPRequest ,1.8.2版本

    我们先创建这个PodFile文件。在终端中进入(cd命令)你项目所在目录,然后在当前目录下,利用vim创建Podfile,运行:

           $ vim Podfile

    然后在Podfile文件中输入以下文字:

           pod "ASIHTTPRequest", "~> 1.8.2"

    然后保存退出。vim环境下,保存退出命令是:

           :wq!

    我们会发现项目中,多了一个名字为Podfile的文件,而且文件内容就是输入的内容。注意,Podfile文件应该和你的工程文件.xcodeproj在同一个目录下。

    下面我们就可以利用CocoPods下载ASIHTTPRequest类库了。还是在终端中的当前项目下,运行命令:

          $ pod install

    运行完命令之后,会看见有这句话[!] From now on use ********.xcworkspace这是说:以后打开项目就用 **.xcworkspace 打开,而不是之前的**.xcodeproj文件。

    现在,我们就可以开始使用ASIHTTPRequest啦。可以测试一下,在项目中输入:
    #import <AFNetworking.h>或者#import "AFNetworking.h"试试

    如果以后有更新 或者 添加别的类库,用update命令即可:

           $ pod update

    CocoaPods 报错

    Xcode 升级到 6.0 后,更新 CocoaPods 或者以前手动添加过第三方SDK改用Cocopods,可能会出现以下的错误

    错误1:
         [!] The `Paopao [Debug]` target overrides the `PODS_ROOT` 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 `Paopao [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 `Paopao [Release]` target overrides the `PODS_ROOT` 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.
    
           [!] The `Paopao [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.

    产生此警告的原因是项目 Target 中的一些设置,CocoaPods 也做了默认的设置,如果两个设置结果不一致,就会造成问题。

    我想要使用 CocoaPods 中的设置,分别在我的项目中定义PODS_ROOT 和 Other Linker Flags的地方,把他们的值用$(inherited)替换掉,进入终端,执行

          pod update

    警告没了

    我个人更喜欢一种简单粗暴的方法:

    点击项目文件 project.xcodeproj,右键显示包内容,用文本编辑器打开project.pbxproj,删除OTHER_LDFLAGS的地方,保存,pod update,警告没了。。。。

    错误2:

    错误

    这个解决也比较简单,
    在Build Setting 中的Other Linker Flags选项中加入$(OTHER_LDFLAGS) 如图:


    解决错误

    编译通过.........

    OS X EI Caption系统下遇到的问题

    升级OS X EI Capiton之后,发现CocoaPods的pod命令无效了,提示一下错误

    pod: command not found

    解决步骤:
    1.卸载

    sudo gem uninstall cocoapods

    2.重新安装

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

    3.如果没有权限执行pod,执行命令一下命令赋予/usr/local/bin给予执行与读取权限

    sudo chmod +rx /usr/local/bin/

    2016-12月补充

    1新增加Cocoapods集成命令U-Share时直接使用pod update更新,不可加入--no-repo-update参数,需直接更新线上Cocoapods库。

    2xcode8打开podfile文件,却说字符非法,

    可以用这句正确的代码替换:pod 'Parse', '~> 1.7.1'

    3bug-反应慢的小bug

      一直报错,重开xcode也没用,后来再setting 搜索user head,还没改东西,就好了。。。。

     
  • 相关阅读:
    Python中下划线---完全解读(转)
    数字、基数及表示
    实现二叉排序树的各种算法
    shell脚本 空格
    Linux中执行shell脚本的4种方法
    vim常用命令总结
    Linux 奇技淫巧
    排序算法之二分治法
    二分查找
    排序算法之一插入排序
  • 原文地址:https://www.cnblogs.com/dzq1991/p/6126608.html
Copyright © 2011-2022 走看看