CocoaPods使用

作者: 紧张的牛排 | 来源:发表于2016-08-13 23:57 被阅读252次

CocoaPods简介

CocoaPods是一个用来管理第三方库的比较好用的管理工具。iOS开发来讲,是不可缺少需要掌握的工具。

本文主要讲解如何利用好CocoaPods来管理项目。为了演示使用,我首先创建一个测试工程,工程名称为testPods,工程结构如下:

测试工程结构

1. 简单使用CocoaPods

1.1 初始化Podfile
  在Terminal中,先进入工程根目录下,然后进行Podfile的初始化。
  pod init初始化Podfile.

初始化后内容如下:


Pod init

1.2 搜索第三方库(以SDWebImage为例)
  pod search SDWebImage

pod search SDWebImage

1.3 编辑Podfile
  Podfile中复制pod 'SDWebImage', '~> 3.8.1',并设置需要的版本,如果不写版本默认为最高版本。

编辑Podfile

1.4 安装
  pod install安装Podfile中的第三方库。

pod install

安装后,有个感叹号,提示现在开始使用testPods.xcworkspace。我们进入工程根目录底下,已经有testPods.xcworkspace工作空间了。打开workspace后工程目录结构如下:

testPods.xcworkspace

1.5 使用SDWebImage,我们以UIImageView+WebCache下载图片为例。代码如下:

#import "ViewController.h"
#import "UIImageView+WebCache.h"

#define kImageAddrUrlStr    @"http://love.doghouse.com.tw/image/wallpaper/011102/bf1554.jpg"

@interface ViewController ()
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initImageView];
}

- (void)initImageView {
    _imageView = [[UIImageView alloc] init];
    _imageView.frame = [UIScreen mainScreen].bounds;
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:_imageView];
}
//点击屏幕时,开始下载
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:kImageAddrUrlStr]];
}```
> 运行程序测试时,如有提示如下请设置工程的配置:
```App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.```

>配置方法:在工程Info.plist中增加如下信息。
![配置Info.plist](http:https://img.haomeiwen.com/i2331407/927ce755187f103b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

> 1.6 运行结果OK.


###2. 修改第三方库的需求时使用CocoaPods
> 有些时候,第三方库不能完全满足我们需求时,需要修改该第三方库了。这种情况下,我们也可以使用CocoaPods管理。完整下载第三方库,并上传到自己的远端仓库里。可以这样理解,仓库的master是完整的第三方库,我们创建一个分支,当做pod的远程仓库。

> ***如何使用Git需要了解,请查阅[git 介绍和使用](http://www.jianshu.com/p/195414576041)***。

> 2.1 下载完整的第三方库
下载后进入根目录下,查看```git status```,并创建分支```MySDWebImage```。
```git checkout -b MySDWebImage```
![git status](http:https://img.haomeiwen.com/i2331407/19b18c3ecb7b4697.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

> 2.2 创建自己的远端仓库
我使用的是gitlab,以gitlab远程仓库为例,我创建的远端仓库地址为```git@gitlab.com:zhang.wenhai/SDWebImage.git```,并设置分支```MySDWebImage```的远端仓库地址。
```git remote add MySDWebImage git@gitlab.com:zhang.wenhai/SDWebImage.git```

> 2.3 上传到自己远端仓库中
```git push MySDWebImage MySDWebImage```
在我的仓库中可以看见一个```MySDWebImage```的分支了。

> 2.4 修改Podfile的下载地址
```pod 'SDWebImage', :git=>'git@gitlab.com:zhang.wenhai/SDWebImage.git', :branch=>'MySDWebImage'```
这样就从我们自己的库中下载SDWebImage了。
![pod install from my gitlab](http:https://img.haomeiwen.com/i2331407/03b36864fdb1e839.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

> 2.5 如果需要修改需求的话,修改后```push```到自己的分支上,再进行```pod install```即可。


###3. 创建自己的库,使用CocoaPods管理
> 3.1 创建自己的库工程,我自己创建的framework工程如下,提供了一个方法```- (void)doIt;```
![WHClass](http:https://img.haomeiwen.com/i2331407/d3d86eeae961ecb0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

> 3.2 上传到远端仓库中,不做讲解,可以查阅[git 介绍和使用](http://www.jianshu.com/p/195414576041)。

> 3.2 初始化创建podspec文件
```pod spec create WHClass```创建podspec模板。
![创建podspec](http:https://img.haomeiwen.com/i2331407/6ddc4fe01d74f9d5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

> 3.3 配置podspec文件
```s.source_files  = "WHClass", "WHClass/**/*.{h,m}"```指定共享文件
```s.source       = { :git => "https://gitlab.com/zhang.wenhai/WHClass.git"}```远端仓库地址
```s.license      = ""```license可以空

>检查podspec配置
```pod spec lint WHClass.podspec ```

>直到没有错误为止,可以有警告。
![pod spec lint WHClass.podspec](http:https://img.haomeiwen.com/i2331407/9e6e049e82292d96.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

> 3.3 ```git push```podspec到远端仓库中。

> 3.4 使用CocoaPods安装
修改Podfile文件,与**2.4**类似。
```pod 'WHClass', :git=>'git@gitlab.com:zhang.wenhai/WHClass.git'```

> 进行```pod install```安装即可。
![pod install](http:https://img.haomeiwen.com/i2331407/91dda86ac6df2142.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


![安装结果](http:https://img.haomeiwen.com/i2331407/27bf6f0b889539a5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


##CocoaPods使用愉快!##


























相关文章

网友评论

    本文标题:CocoaPods使用

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