Bo2SS

Bo2SS

(5) Introduction to iOS: Overview and Use of CocoaPods

Beautiful sea comes from a beautiful sky

Beautiful sea comes from a beautiful sky

Finally, we have reached the last section of our "Xia Piao Piao" introductory iOS series—Introduction and Use of CocoaPods.

image

Without further ado, I will begin explaining according to the four steps outlined above.

What is it, why use it, and how to use it?#

What is CocoaPods❓

According to the official explanation, it is a dependency management tool for third-party libraries used in developing iOS applications.


Why use it❓

Because it can automate, centralize, and intuitively manage third-party open-source libraries, which can be compared to gradle, npm, etc.

PS: The common framework integration methods for iOS are compared below, refer to "Geek Time".

From "Geek Time"


How to use it❓

  1. First, you need to install it~
    1. Prerequisite—Install Ruby: brew install ruby, CocoaPods is built on Ruby;
    2. Install CocoaPods: sudo gem install -n /usr/local/bin cocoapods, gem is the package manager for Ruby modules;
    3. Install the spec repository❗️ locally: pod setup, the spec repository is quite large, have a cup of ☕️ and wait~
  2. The usage steps are as follows:
    1. cd [project root directory]
    2. Initialize pod: pod init, which will generate a Podfile;
    3. Edit the Podfile❗️
    4. Integrate the corresponding code library❗️ into the project: pod install
    5. When using, just #import <header file>.

You may have noticed the three ❗️ above, which are the key points of today 🎺🎺🎺. Let's continue with the following two questions:

  1. How to edit the Podfile?
  2. What are the spec repository and code library, and what is the difference between them? You can temporarily understand them as a manual and a physical object.

First, let's look at the first question: How to edit the Podfile?

Editing the Podfile#

The basic template of the Podfile is as follows:

platform :ios, '9.0'   # Specify the device platform "iOS" and the minimum supported version "9.0"
inhibit_all_warnings!  # Suppress all warnings
use_frameworks!        # Use dynamic libraries, otherwise use static libraries

target 'MyApp' do                   # target specifies the target name
  pod 'Masonry',                    # Import the latest version of Masonry
  pod 'YYModel', '1.0.4'            # Import version 1.0.4 of YYModel
  pod 'ObjectiveSugar', '>= 0.5.2'  # Import the latest version of ObjectiveSugar>=0.5.2

  target 'MyAppTests' do           # Nested test target
    inherit! :search_paths         # Inherit the search paths from the above (must include the aforementioned third-party libraries)
    pod 'OCMock', '~> 2.0.4'       # Import OCMock>=2.0.4 and <2.1 latest version
  end
end
  • Note the version matching rules and the technique of nesting targets to reduce repetitive pod writing; more details can be found in the comments.

Having learned the simple method of writing a Podfile, let's see which third-party libraries are used in "Xia Piao Piao"👇

Can you imagine what the Podfile looks like?👇

source 'https://github.com/CocoaPods/Specs.git'  # You can manually specify the address of the spec repository
platform :ios, '9.0'
use_frameworks!

target 'ShoPiaoPiao_Example' do
  pod 'ShoPiaoPiao', :path => '../'              # Pull local library by setting :path=> 

  pod 'Masonry'                                  # The six third-party libraries mentioned above
  pod 'AFNetworking'
  pod 'SDWebImage'
  pod 'YYModel'
  pod 'MJRefresh'
  pod 'MBProgressHUD'
end
  • The manually specified spec repository can be a private Git repository of a company or individual. ⚠️: If you manually specify other libraries, you must specify all the spec repositories you want to use, including the default spec repository (installed for the first time), as shown in the code above.
  • You can pull local libraries by setting =>, which is often used for developing that library.
  • The third-party libraries here all default to using the latest version.

A warm reminder 🎺: You might be wondering what the ShoPiaoPiao third-party library is. It belongs to the "Xia Piao Piao" project. Why is it imported locally as a pod? Actually, I am developing the "Xia Piao Piao" project in the way of developing a third-party library, which is commonly done in the company, similar to SDK development. The section on common commands below will also mention how to create a third-party library based on CocoaPods.

Illustrated Principles#

Referencing three images from the internet to explain the basic principles of CocoaPods.

First, let's answer the second question mentioned earlier: What are the spec repository and code library, and what is the difference between them?

Look at the image:

Find the Spec repository and code library

  1. By running pod setup, you install the spec repository locally, which actually pulls the remote CocoaPods official index repository to your local machine. This index repository stores the description information of various third-party libraries, each stored in a spec file, which contains the following information about the third-party library:
  • Library name
  • Version number
  • Description
  • Source code address❗️
  • ...
  1. When we have written the Podfile and run pod install, CocoaPods will fetch the source code from the source code address of each library and integrate it into the project. (Of course, this source code will be cached)

After this, do you understand the difference between the spec repository and the code library?


Now let's look at two images about the pod install process:

pod install process-1

pod install process-2

Try to understand the pod install process by following the numbers and arrows. If you can understand the explanation above, understanding these two images should not be a problem~🤔

Common Commands#

Here is a list of common commands that can be saved as a reference tool.

Common CocoaPods Commands


🥩Extra Meal: pod install VS. pod update

What is the difference between them? This is probably the most common confusion when using CocoaPods for the first time.

Similarities:

  • The purpose is to pull the code libraries that the project depends on
  • Update Podfile.lock

Differences:

  • The former is constrained by Podfile.lock, while the latter is not
  • The latter will update the local spec repository

Detailed Process:

pod install

Checks whether Podfile.lock already contains the libraries in the Podfile;

(1) If it contains, it continues to check whether the version is specified in the Podfile: 1) If a version is specified, it checks whether the version saved in Podfile.lock is the same as the one specified in the Podfile; if they are the same, it skips; if they are different, it updates to the version specified in the Podfile; 2) If no version is specified, it skips without checking for updates;

(2) If the library is not included, it downloads the library and saves the version in the Podfile.lock file.

PS: Podfile.lock generally applies to projects where no version is specified in the Podfile.


pod update

Ignores the records in Podfile.lock, first updates the spec repository, and then directly looks for the specified version of the dependent library in the Podfile. If no version is specified, it will look for the latest version.

PS: pod update [PODNAME] specifies a certain Pod library to update.

References#

What is it, why use it, and how to use it?

How do I install CocoaPods?——stackOverflow

CocoaPods Guides——Official

Understanding cocoapods from the documentation on Podspec——Jianshu


Editing the Podfile

Do you really know how to write a Podfile?——Juejin

inherit!——Official


Common Commands

Difference between pod install and pod update——Jianshu

Conclusion#

After nearly a month, I have completed the "Introduction to Xia Piao Piao iOS" series. I hope you have gained something by reading this, or you can implement "Xia Piao Piao" yourself. Feel free to share your results with me~

In the future, I will also organize a portal to share the source code of "Xia Piao Piao" and make a summary ✅.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.