Xcode中安装CocoaPods

来源:互联网 发布:网络存储器 编辑:程序博客网 时间:2024/06/03 22:51

注:本文是在Mac电脑中已经安装完成好Ruby环境的前提下进行的。xcode版本7.0.1

   1.修改Ruby镜像

因为cocoapods.org已经被屏蔽了,使用源网站的话要翻过去。所有为了保证下载的速度,需要切换Ruby镜像。

先删除cocoapods.org镜像:


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

$gem sources –a  https://ruby.taobao.org/ (注:网上很多教程使用的是http://ruby.taobao.org/,但是淘宝已经停用了http服务的协议,需要使用https协议代替)

$gem sources –l (用该命令查看ruby镜像是否是taobao.com)

显示:

*** CURRENT SOURCES ***


https://ruby.taobao.org/

2.安装CocoaPods

$sudo gem install cocoapods 

输入完密码后即开始安装。

3.验证CocoaPods是否安装成功

$ pod



localhost:~ fudong$ pod

Usage:


    $ pod COMMAND


      CocoaPods, the Cocoa library package manager.


Commands:


    + cache      Manipulate the CocoaPods cache

    + init       Generate a Podfile for the current directory.

    + install    Install project dependencies to Podfile.lock versions

    + ipc        Inter-process communication

    + lib        Develop pods

    + list       List pods

    + outdated   Show outdated project dependencies

    + plugins    Show available CocoaPods plugins

    + repo       Manage spec-repositories

    + search     Search for pods.

    + setup      Setup the CocoaPods environment

    + spec       Manage pod specs

    + trunk      Interact with the CocoaPods API (e.g. publishing new specs)

    + try        Try a Pod!

    + update     Update outdated project dependencies and create new Podfile.lock


Options:


    --silent     Show nothing

    --version    Show the version of the tool

    --verbose    Show more debugging information

    --no-ansi    Show output without ANSI codes

    --help       Show help banner of specified command

localhost:~ fudong$ 


4.使用CocoaPods搜素第三方库

localhost:~ fudong$ pod search "AFNetworking"

第一次搜索的时候会出现Setting up CocoaPods master repo,然后终端会卡住,这是正常情况,说明在下载一些信息,打开另一个终端输入:

localhost:~ fudong$ cd ~/.cocoapods

localhost:.cocoapods fudong$ du -sh *

279M repos

localhost:.cocoapods fudong$ 

多执行几次du -sh *查看该文件夹的大小,我的是2015年11月29日更新的,大小为279M,请耐心等待。
最后下载完成,会显示出搜索出来的AFNetworking三方库的情况

localhost:~ fudong$ pod search "AFNetworking"



-> AFNetworking (3.0.0-beta.2)

   A delightful iOS and OS X networking framework.

   pod 'AFNetworking', '~> 3.0.0-beta.2'

   - Homepage: https://github.com/AFNetworking/AFNetworking

   - Source:   https://github.com/AFNetworking/AFNetworking.git

   - Versions: 3.0.0-beta.2, 3.0.0-beta.1, 2.6.3, 2.6.2, 2.6.1, 2.6.0, 2.5.4, 2.5.3,

   2.5.2, 2.5.1, 2.5.0, 2.4.1, 2.4.0, 2.3.1, 2.3.0, 2.2.4, 2.2.3, 2.2.2, 2.2.1, 2.2.0,

   2.1.0, 2.0.3, 2.0.2, 2.0.1, 2.0.0, 2.0.0-RC3, 2.0.0-RC2, 2.0.0-RC1, 1.3.4, 1.3.3,

   1.3.2, 1.3.1, 1.3.0, 1.2.1, 1.2.0, 1.1.0, 1.0.1, 1.0, 1.0RC3, 1.0RC2, 1.0RC1,

   0.10.1, 0.10.0, 0.9.2, 0.9.1, 0.9.0, 0.7.0, 0.5.1 [master repo]

   - Subspecs:

     - AFNetworking/Serialization (3.0.0-beta.2)

     - AFNetworking/Security (3.0.0-beta.2)

     - AFNetworking/Reachability (3.0.0-beta.2)

     - AFNetworking/NSURLSession (3.0.0-beta.2)

     - AFNetworking/UIKit (3.0.0-beta.2)



-> AFNetworking+AutoRetry (0.0.5)

   Auto Retries for AFNetworking requests

   pod 'AFNetworking+AutoRetry', '~> 0.0.5'

   - Homepage: https://github.com/shaioz/AFNetworking-AutoRetry

   - Source:   https://github.com/shaioz/AFNetworking-AutoRetry.git

   - Versions: 0.0.5, 0.0.4, 0.0.3, 0.0.2, 0.0.1 [master repo]



5.安装完成,在项目中使用它
首先先新建一个项目(我新建了一个名为FDCocoaPods的项目)
新建完成后,打开终端,进入项目所在的位置,新建一个名字为Podfile的文件,必须是这个名字

localhost:~ fudong$ cd Desktop/FDCocoaPods/

localhost:FDCocoaPods fudong$ vi Podfile

localhost:FDCocoaPods fudong$ 

在文件中添加文字如下:
platform:ios,'7.0'
pod "AFNetworking","~> 2.0"

注:该内容是根据需要使用的第三方库的响应信息添加的,通过pod search + 第三方库 命令可以查找到响应的信息,此处主要输入法的问题,不然会报错。
输入完成后保存退出。
在终端中继续输入如下命令:

localhost:FDCocoaPods fudong$ pod install

Updating local specs repositories

Analyzing dependencies

Downloading dependencies

Installing AFNetworking (2.6.3)

Generating Pods project

Integrating client project


[!] Please close any current Xcode sessions and use `FDCocoaPods.xcworkspace` for this project from now on.

Sending stats

Pod installation complete! There is 1 dependency from the Podfile and 1 total

pod installed.

localhost:FDCocoaPods fudong$ 

此时,打开项目所在的文件夹中可以看到比新建的时候多出来了数个文件,打开"项目名.xcworkspace",便打开了支持CocoaPods的项目。


6.在xcode中添加配置

在项目配置中找到Build Settings-->User Header Search Paths中双击添加一个新的项目 添加上“#(PODS_ROOT)”,第二个选项选中recursive


添加完成之后到文件中就可以导入AFNetworking头文件了。


至此,CocoaPods安装与使用结束。


0 0