CocoaPods install and use

When you develop iOS applications, it will often use a lot of third party open source libraries, such as JSONKit, AFNetWorking and so on. May use a library and other libraries, so to use it, you have to download additional other libraries, and other libraries and use other libraries. In short, one by one manually to download the required library is very troublesome. Another common scenario is that you used in the project library is updated, you have to re-download the new version, re-added to the project, is very troublesome. This time, you need CocoaPods.

CocoaPods should be the most common and well-known iOS library management tools, and these two annoying problem by cocoaPods, only one command can be completely solved, of course, the premise is you have to set it right. Importantly, most of the well-known open-source library, support CocoaPods. So, as we iOS programmer to master the use of CocoaPods is essential basic skills.

Download and install

Before installing CocoaPods, you must first install the Ruby environment locally. As for how to install the Ruby environment on a Mac, please google it, this is no longer involved.

If you have already installed Ruby in the local environment, then download and install CocoaPods will be very simple and requires only one command. Enter the following command in the terminal:

sudo gem install cocoapods
But, wait a minute. If you're after, typing this command in a terminal in the heavenly, you will find for a long time nothing happens. Because without him, because that wall blocking cocoapods.org.

But, yes, and it (but a welcome "but"). We can use Taobao Ruby mirrored to access cocoapods. Knock the following order in order to enter the command in the terminal:

GEM Sources -remove $ https://rubygems.org/
and then typing the following commands, which are reaction after //
$ GEM Sources -a http://ruby.taobao.org/
In order to verify your Ruby is mirrored and only taobao , you can use the following command to view:

$ Gem sources -l
only the following text appears in the terminal only show you the above command is successful:

* CURRENT SOURCES *

http://ruby.taobao.org/
all the above command completion, you again run in a terminal:

$ Sudo gem install cocoapods
wait a few seconds, CocoaPods can be downloaded and installed in your local, no longer need to other settings.

use

Well, installed after CocoPods, the next step is to use it. Fortunately, the use of CocoPods and install it as simple, but also through a two-line command can handle that.

Scene 1: using CocoaPods, introduced in the project library AFNetworking

AFNetworking library on GitHub address is: https://github.com/AFNetworking/AFNetworking

In order to determine whether AFNetworking support CocoaPods, you can use the search function CocoaPods verify. In the terminal type:

$ pod search AFNetworking

After a few seconds, you will see some information about AFNetworking library in the terminal.

This shows that, AFNetworking support CocoaPods, so we can use CocoaPods will AFNetworking import your project.

First of all, we need to add support CocoaPods in our project. First use Xcode to create a name CocoaPodsDemo project for the following tutorial.

You see here may ask, CocoaPods why can download AFNetworking it, other libraries instead of downloading it? The answer to this question is that there is a file that controls CocoaPods what to download. This file is called "Podfile" (note, must be the file name, and there is no suffix). Podfile you create a file, you need to download and then add the library on the inside, which tells CocoaPods, "so and so and so and so and so and so, coming to bowl!." Each project requires only a Podfile file.

Well, without further ado, let's create the magic PodFile. Entering (cd command) directory of your project in the terminal, and then in the current directory, use vim create Podfile, run:

$ vim Podfile

Then enter the following text Podfile file:

platform :ios, '7.0'
pod "AFNetworking", "~> 2.0"

Note that this text is not produced out of thin air, can be found in AFNetworking's github page. These two words mean, up to the current iOS version AFNetworking support is iOS 7.0, AFNetworking to download version 2.0.

Then save and exit. vim environment, save and exit the command is:

: wq
this time, you will find your project directory, there's a name for Podfile file, and the file content is the content that you just entered. Note, Podfile file and you should .xcodeproj project files in the same directory.

At this time, you can use CocoPods download AFNetworking the library. Or in the terminal project in the current directory, run the following command:

$ Pod install
because it is imported AFNetworking in your project, which is why this command requires you to enter the directory where your project is running.

After running the above command terminal the following information:

EricmatoMacBook-Pro:CocoaPodsDemo ericwang$ pod install
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (2.0.2)
Generating Pods project
Integrating client project

[!] From now on use `CocoaPodsDemo.xcworkspace`.

Note the last sentence, which means: to open the project after it opened with CocoaPodsDemo.xcworkspace, rather than .xcodeproj previous file.

You may be depressed, why is there .xcodeproj file it. This is what you have just run the new file generated $ pod install command. In addition to this file, you will find more than another file "Podfile.lock" and a folder "Pods". After clicking CocoaPodsDemo.xcworkspace then open the project, you will be pleasantly surprised, AFNetwoking the project has been successfully imported.
Now, you can start using AFNetworking.h friends. Can be a little test, enter arbitrary code in your project file:

#import "AFNetworking.h"

Then compiled, to see whether an error occurred.

At this point, CocoPods first scenario tells completed. Process boils down to this:

Create Podfile in the project, Podfile content that you want to import libraries. General library author will tell you how to import the library should write Podfile;
run the command: `$ pod install.

Scene 2: How to compile and run properly library project contains a CocoPods

You may have encountered (especially new iOS developer) this case, hard to find a code on GitHub want to meet their needs, and excitedly downloaded, a compiler, dumbfounded, I found a variety of errors . I saw that it was missing a variety of other third-party libraries. Mo panic this time, and then you look closely, you will find you download the code contains Podfile. Yes, this means you can use CocoaPods easily download the required libraries.

Here, code-UAAppReviewManager example to illustrate how to properly compile and run a CocoPods class library project contains.

UAAppReviewManager is one that allows you to easily remind a user score of functionality to your application. When you go to the GitHub address UAAppReviewManager download this code opens Example engineering (UAAppReviewManagerExample), compile, you will find Xcode reporting a lot of mistakes, you are basically saying this code is compiled header can not find a certain file, This means that you have to succeed UAAppReviewManager Example code is compiled, you must import some third-party libraries. Also you'll find here are three with CocosPods related files (folders) in UAAppReviewManagerExample folder: Podfile, Podfile.lock and Pods, at this time, open a terminal, go to the directory where the UAAppReviewManagerExample, that is, and Podfile in the same directory, and the same scene 1, enter the following command (because they have had Podfile, so do not need to create Podfile):

$ pod update

After a few seconds (may need ten seconds, depending on your network conditions), the terminal appears:

Analyzing dependencies
Fetching podspec for `UAAppReviewManager` from `../`
Downloading dependencies
Installing UAAppReviewManager (0.1.6)
Generating Pods project
Integrating client project

[!] From now on use `UAAppReviewManagerExample.xcworkspace`.

This time, look back to UAAppReviewManagerExample folder, you will see more than one file UAAppReviewManagerExample.xcworkspace:

According to information suggesting the terminal, UAAppReviewManagerExample.xcworkspace later you will need to use the newly generated code to run this Example.

Open UAAppReviewManagerExample.xcworkspace, compile and run, success!

Note that there is a small problem, if you are not just enter the $ pod update, but enter $ pod install, will find the library import is unsuccessful, and the terminal prompt appears below:

[!] Required version (UAAppReviewManager (from `../`)) not found for `UAAppReviewManager`.
Available versions: 0.1.6

The meaning here is probably Podfile file expires, libraries have to upgrade, but Podf large columns  CocoaPods install and use ile has not changed. $ Pod install only in accordance with the requirements Podfile to request library, if the library version number change, it will fail to obtain. But $ pod update will update all of the libraries for the latest version of the library. And you will find that if the $ pod update, and then $ pod install success.

Then you might ask, when to use $ pod install, when to use $ pod update it, I do not know that there is no new version of the library. Well, every time you forget the direct use of $ pod update. Or first with $ pod install, and if not, then $ pod update.

Scenario 3: CocoaPods develop and package a static library

Usually we use pod to manage 3rd party open source libraries, but we are also very likely to develop a dependency management with pod static library for others to use, but do not want open source, such as some of the SDK, you will need to packaged into .a file. This will be a ASIHTTPRequest rely on static libraries, and to demonstrate how to create a static library CocoaPods and packaging process.

Developing a static library (Static Library)

Create a static library, there are two methods.

Created manually (deprecated)

Process more complicated, pure manual labor is not recommended, said the next step in general

  1. Creating a Cocoa Touch Static Library in Xcode;
  2. Creating Podfile file;
  3. Execution pod install complete build of the entire project;
    if you need demo, manually create the sample program, using the pod add a reliance on private static library, repeat the pod install complete sample project to build.

Created automatically based on pod

Only you need to enter the pod lib commands to complete the initial set up of the project, described in detail the steps below to WaterDropView as a demonstration project name.

1. Run pod lib create WaterDropView. In the meantime we need to confirm the following four questions.
What language do you want to use?? [ ObjC / Swift ]
> Objc

Would you like to include a demo application with your library? [ Yes / No ]
> Yes

Which testing frameworks will you use? [ Specta / Kiwi / None ]
> None

Would you like to do view based testing? [ Yes / No ]
> No 

What is your class prefix?
> ZM

Running pod install on your new library.

Analyzing dependencies

CocoaPods 0.39.0.beta.4 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.

Fetching podspec for `WaterDropView` from `../`
Downloading dependencies
Installing WaterDropView (0.1.0)
Generating Pods project
Integrating client project

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

Ace! you're ready to go!
We will start you off by opening your project in Xcode
open 'WaterDropView/Example/WaterDropView.xcworkspace'

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.

The first question asks whether a demo project, usually choose Yes, the other can choose according to need. After the command is executed, it will create a good framework for a basic class library dependencies by cocoapods management.

2. Open WaterDropView.podspec file, modify the library configuration information, results like this.
#
# Be sure to run `pod lib lint WaterDropView.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
  s.name             = "WaterDropView"
  s.version          = "0.1.0"
  s.summary          = "A short description of WaterDropView."

# This description is used to generate tags and improve search results.
#   * Think: What does it do? Why did you write it? What is the focus?
#   * Try to keep it short, snappy and to the point.
#   * Write the description between the DESC delimiters below.
#   * Finally, don't worry about the indent, CocoaPods strips it!  
  s.description      = <<-DESC
                   DESC

  s.homepage         = "https://github.com/qcc107/WaterDropView"
  # s.screenshots     = "www.example.com/screenshots_1", "www.example.com/screenshots_2"
  s.license          = 'MIT'
  s.author           = { "qcc107" => "[email protected]" }
  s.source           = { :git => "https://github.com/qcc107/WaterDropView.git", :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.platform     = :ios, '7.0'
  s.requires_arc = true

  s.source_files = 'Pod/Classes/**/*'
  s.resource_bundles = {
  'WaterDropView' => ['Pod/Assets/*.png']
  }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end

By default configuration, the library source file located Pod / under Classes folder, the resource files are located in Pod / Assets folder can be modified to replace s.resource_bundles s.source_files and store directory. s.public_header_files to specify the search location for header files.
s.frameworks and s.libraries specified dependency of the SDK framework and libraries, need to pay attention, not only to include dependencies rely on your own libraries, but also include all dependent on third-party libraries, the only way when your class .a library packaged into other projects or .framework only make normal use.

3. Go to the Example folder, perform the pod install, let demo project dependencies to install and update the configuration.
$pod install

Analyzing dependencies

CocoaPods 0.39.0.beta.4 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.

Fetching podspec for `WaterDropView` from `../`
Downloading dependencies
Using WaterDropView (0.1.0)
Generating Pods project
Integrating client project
4. Add code.

Add ZMWaterDropView class, pay attention to where the file is stored in the Pod / Classes directory, to be consistent with podspec configuration.
Run Pod install, let demo program loads the new class. Perhaps you have discovered, as long as the newly added class / resource files or rely tripartite libraries need to rerun Pod install to apply the updates.
Example write code.

Submit native code libraries

1. Modify s.source. Modified according to your actual path.

= {s.source: Git => "/ the Users / name / Workspace / ZMWaterDropView",: Tag => '0.1.0'}
2. Submit source, and playing tag.

the Add Git.
Git the commit -a -m 'v0.1.0'
Git 0.1.0 Tag -a -m 'v0.1.0'
authentication library

After the development is completed static library, you need to run pod lib lint library to test compliance with the requirements of the pod. You can ignore the warning by adding -only-errors.

-only-lib lint ZMWaterDropView.podspec POD errors -verbose
...
ZMWaterDropView passed Validation.
packaged class libraries

Cocoapods need to use a plug-cocoapods-packager to complete the package library. Of course, you can manually compile the package, but the process can be quite tedious.

Plug-in installation package
terminal execute the following command

sudo gem install cocoapods-packager
package
command is very simple, execution

pod package ZMWaterDropView.podspec -library -force
wherein -library packaged into .a specified file, if the tape will not be packaged into .framework file. -Force refers to mandatory coverage. Needs to be stressed is that the plug-in library referenced by the tripartite rename a good solution to the problem of library naming conflicts.


Reference herein code4app and Bryce Zhang .

Guess you like

Origin www.cnblogs.com/sanxiandoupi/p/11712808.html