美文网首页
Cocoapods 安装

Cocoapods 安装

作者: HelloRaymond | 来源:发表于2016-03-10 11:50 被阅读0次

1. 安装 Cocoapods

CocoaPods 是基于 Ruby 构建的,Mac OS X 操作系统默认已安装 Ruby 的。所以,我们可以直接使用Ruby命令 gem 来安装 Cocoapods,安装命令如下。

$ sudo gem install cocoapods

经过一段时间的等待后,会提示安装成功的信息。

Successfully installed cocoapods-0.39.0
Parsing documentation for cocoapods-0.39.0
1 gem installed

此时,运行 pod 命令执行一些相关操作。例如,我想查看帮助。

$ pod --help

2. 使用 Cocoapods 安装第三方类库

打开 Xcode 新建一个项目,例如我创建一个名为 HelloWorld 的项目,创建完毕之后,打开终端使用 cd 命令进入 HelloWorld 项目目录里。然后,你可以手动创建 Podfile 文件或者通过 pod 命令来创建也可以。

$ pod init

打开已创建好的 Podfile 文件,编写一些安装信息,详情请查看官方文档。这里就以安装 AFNetworking 库作为示例,安装之前请退出当前 App 项目。

# Podfile:
platform :ios, '8.0'
use_frameworks!

target do
  pod 'AFNetworking', '~> 2.6'
end

编写完成之后,保存退出并运行命令来执行安装。

$ pod install

经过一段时间的等待,会提示一些已安装完成的信息。

CocoaPods 1.0.0.beta.5 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.

Analyzing dependencies
Downloading dependencies
Installing AFNetworking (2.6.3)
Generating Pods project
Integrating client project

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

此时,开打App项目 app.xcworkspace, 而再不是 app.xcodeproj,手动或者使用 open 命令开打。

$ open HelloWorld.xcworkspace

最后,你可以在开始导入你的第三方类库了。例如:

//  ViewController.m
//  HelloWorld
//
//  Created by Raymond on 16/3/10.
//  Copyright © 2016年 YESHM. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

注意,你可能使用 #import 导入文件时,在文件列表也许会没有提示 AFNetworking.h 文件,但你可以先忽略这点,依然手动输入整个文件的名称。然后在 viewDidiLoad 中尝试使用此库的方法,你会发现是可以正常使用的,并且 ⌘ + B 是 Build Successed。

相关文章

网友评论

      本文标题:Cocoapods 安装

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