为iOS项目集成CocoaPods支持

来源:互联网 发布:linux打开html文件 编辑:程序博客网 时间:2024/05/29 06:41

CocoaPods为我们的项目管理依赖库,依赖库可以很容易的通过一个叫做Podfile的文件来管理,通过简单的几句命令行代码来同步更新你的依赖库。CocoaPods简化了我们添加第三方依赖库的复杂度,使得代码的可读性更强,管理第三方库更容易。

安装

CocoaPods的运行需要有Ruby环境,因此在我们安装CocoaPods之前,首先需要为我们的MacOS安装Ruby环境。

Ruby环境搭建

由于国内强大的防火墙,直接安装Ruby可能导致不成功,因此这里我们使用Ruby镜像指向taobao的地址https:ruby.taobao.org/(貌似原来的http地址已经无法使用了)。

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

安装完成后,我们可以使用代码来检查Ruby安装的版本,使用 ruby -v

Taylorwen-MacBook-Pro: wenzhan$ ruby -vruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin15]

更新Ruby版本

$ sudo gem update --systemUpdating rubygems-updateFetching: rubygems-update-2.6.6.gem (100%)Successfully installed rubygems-update-2.6.6Parsing documentation for rubygems-update-2.6.6Installing ri documentation for rubygems-update-2.6.6Installing darkfish documentation for rubygems-update-2.6.6Installing RubyGems 2.6.6RubyGems 2.6.6 installedParsing documentation for rubygems-2.6.6Installing ri documentation for rubygems-2.6.6=== 2.6.6 / 2016-06-22RubyGems system software updated

通过sudo来安装gems

通过默认的Ruby来安装CocoaPods的gems,需要使用sudo命令。

$ sudo gem install cocoapods

安装过程可能会持续半分钟,成功后会显示你安装的gems的名称和数量;

Fetching: cocoapods-core-1.0.1.gem (100%)Successfully installed cocoapods-core-1.0.1Fetching: claide-1.0.0.gem (100%)Successfully installed claide-1.0.0Fetching: cocoapods-deintegrate-1.0.0.gem (100%)Successfully installed cocoapods-deintegrate-1.0.0Fetching: cocoapods-downloader-1.1.0.gem (100%)Successfully installed cocoapods-downloader-1.1.0Fetching: cocoapods-plugins-1.0.0.gem (100%)Successfully installed cocoapods-plugins-1.0.0.........14 gems installed

验证一下你安装的CocoaPod版本号

Taylorwen-MacBook-Pro: wenzhan$ pod --version1.0.1

如果需要升级你的CocoaPods,你只需要重新安装CocoaPods的gems

$ sudo gem install cocoapods

如果想要安装上一个版本的CocoaPods,使用如下命令

$ sudo gem install cocoapods --pre

使用CocoaPods安装依赖库

CocoaPods通过一个.Podfile来管理你的依赖库,那么为你的项目添加CocoaPods支持后,并没有这个Podfile,因此我们需要创建一个Podfile,这里我使用的是通过命令行创建Podfile方式,当然也可以使用文本编辑工具创建这个文件然后拖放到项目里的/pods文件夹目录中。

$ pod init

这时你的项目文件中就会出现一个Podfile文件。

搜索并安装最新版AFNetworking

$ pod search AFNetworking-> AFNetworking (3.1.0)   A delightful iOS and OS X networking framework.   pod 'AFNetworking', '~> 3.1.0'   - Homepage: https://github.com/AFNetworking/AFNetworking   - Source:   https://github.com/AFNetworking/AFNetworking.git   - Versions: 3.1.0, 3.0.4, 3.0.3, 3.0.2, 3.0.1, 3.0.0, 3.0.0-beta.3     - AFNetworking/Serialization (3.1.0)     - AFNetworking/Security (3.1.0)     - AFNetworking/Reachability (3.1.0)     - AFNetworking/NSURLSession (3.1.0)     - AFNetworking/UIKit (3.1.0)

在Podfile中添加AFNetworking, 最新版CocoaPods对Podfile做了严格的格式要求,一定要按照下面的格式写Podfile

platform :ios, ‘7.0’use_frameworks!target 'YourProjectName' do     (这里写项目名)    pod 'AFNetworking', '~> 3.1.0'end

然后使用命令行执行安装

pod install

pod install命令会对未安装的依赖库进行安装,同时会检查已经安装了的依赖库的版本,如果有新的版本存在,则会对其进行update. 如果我们希望快速安装依赖库而不进行更新的话,使用如下命令行:

pod install --verbose --no-repo-update     (不执行update)

————————————- 分割线 ——————————————-
2016-07-14更新
淘宝已经关闭HTTP协议的镜像服务,改为HTTPS协议。
淘宝ruby地址:https://ruby.taobao.org/

0 0
原创粉丝点击