iOS-Objective-CCocoaPods

CocoaPods使用

2017-07-13  本文已影响10人  追沐

一、What is CocoaPods ?

引用官网上的说法:CocoaPods is built with Ruby and is installable with the default Ruby available on OS X. We recommend you use the default ruby.

CocoPod使用Ruby写的,所以安装时需要更新Ruby环境。

二、安装Cocoapods

1、升级Ruby环境

sudo gem update --system

如果已经安装了,会提示Latest version currently installed. Aborting.

Mac OSX 10.11之后用

sudo gem update -n /usr/local/bin —system

2、安装CocoaPods要访问cocoapods.org,该网站国内不能访问,需要用淘宝镜像来安装

2.1

gem sources --removehttps://rubygems.org/

2.2

gem sources -ahttp://ruby.taobao.org/

2.3

gem sources -l

3、安装Cocoapods

sudo gem install cocoapods

二、install

安装CocoPods也挺简单,直接在终端中输入

sudo gem install cocoapods

回车。输入密码再回车,如果安装成功,则会显示Successfully installed cocoapods-1.2.1,如果之前安装过,会更新。

如果报错,Mac OSX 10.11之后,用下面命令

sudo gem install -n /usr/local/bin cocoa pods

三、创建一个有CocoaPods管理的项目

Tip: CocoaPods provides a pod init command to create a Podfile with smart defaults. You should use it.

第一步:创建Podfile文件

终端cd到项目文件下,执行

pod init

操作,然后打开项目文件看一下生成了一个名为Podfile的文件。打开文件可以看到如下信息

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'PodApp' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for PodApp

end

其中PodApp是项目的target,platform是支持的平台,一般是最小兼容版本,这个我们可以改,我改成了8.0,表示pod里的三方库需最低兼容iOS8。

第二步:Now you can install the dependencies in your project:

终端执行命令

pod install

然后关闭Xcode,打开项目,项目文件中除了之前执行pod init的时候生成的Podfile文件外,还多了两个文件,一个Podfile.lock文件,一个PodApp.xcworkspace,用PodApp.xworkspace打开项目,项目目录也有变化。

到这里就行了,就已经创建了一个由CocoaPods来管理的App项目。

四、项目中导入三方库

上述步骤已经完成的前提下,以AFNetwork为例子:

pod search AFNetworking

执行上命令行查看AFNetworking相关的版本历史。
打开Podflie文件直接在里面添加,pod “AFNetworking”,”3.1.0”,3.1.0是AF的版本号。
然后执行下面命令,执行完后打开项目看看,AF的库已经导入项目里了。

pod install

参考:http://www.cocoachina.com/bbs/read.php?tid-193398-page-1.html

上一篇下一篇

猜你喜欢

热点阅读