iOS CocoaPods integration & usage

What is CocoaPods

CocoaPods is a dependency management tool for iOS projects. Generally, when developing iOS projects, you need to use CocoaPods to manage third-party components and components of private libraries.

https://guides.cocoapods.org

CocoaPods installation

CocoaPods can be installed using RubyGems that comes with the Mac system.

Before installing CocoaPods, you generally need to update the Ruby image:

1

2

3

// 移除淘宝源

gem sources --remove  https://ruby.taobao.org/ 

gem source -a https://gems.ruby-china.com

Open Terminal and enter the following command to install:

1

sudo gem install cocoapods

CocoaPods usage

Podfile

Pofile files detail the dependencies of targets in one or more projects.

An example of a podfile configuration:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '6.0'

inhibit_all_warnings!

xcodeproj 'MyProject'

pod 'ObjectiveSugar''~> 0.5'

target :test do

  pod 'OCMock''~> 2.0.1'

end

post_install do |installer|

  installer.pods_project.targets.each do |target|

    puts #{target.name}

  end

end

Dependencies

Podfile describes the dependencies used in each user target

  • pod is a way to declare specified dependencies
  • podspec provides a simple API for creating podspec
  • target allows you to limit dependencies in the project to only take effect in specified targets.

pod

Specify a dependency of the project. A dependency is declared by a pod name and optionally a version number.

1

2

3

4

5

6

// 忽略版本号,指定到最新的版本

pod 'YYModel'

// 固定某一个版本号

pod 'YYModel''1.0.0'

In addition to specifying or not specifying the version number, you can do the following operations:

>0.1 Any version higher than 0.1

>=0.1 version 0.1 and any higher version

<0.1 any version below 0.1

<=0.1 version 0.1 and any lower version

~>0.1.2 Version 0.1.2 to version 0.2, excluding 0.2

Build Configurations

By default, dependencies are installed in all target build configurations. For scheduling or other reasons, dependencies can only be enabled in the specified target.

1

pod 'Doraemonkit''1.0.0', :configurations => ['Debug','ADHoc']

Subspec

When a Pod is declared to be installed, it will install all default subspecs defined in the podspec.

1

2

3

4

5

6

// 可以这样申明

pod 'QueryKit/Attribute'

// 也可以这样申明

pod 'QueryKit', :subspec => ['Attribute''QuerySet']

Using the files from a local path

If you want to use a local Pod developed by yourself, you can use the path option:

1

pod 'IMModule', :path => '/Users/Desktop/Work/Project/IMModule'

From a podspec in the root of a library repository (referencing the podspec in the root directory of the warehouse)

Use the master branch in the repository:

1

pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git'

Use other branches in the repository:

1

pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :branch => 'dev'

Use a tag in the warehouse: (recommended)

1

pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :tag => '0.7.0'

Use a commit record in the repository:

1

pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :commit => '082f8319af'

Guess you like

Origin blog.csdn.net/yezuiqingxin/article/details/120367804