Bo2SS

Bo2SS

(5) iOS Beginner's Guide: Introduction and Usage of CocoaPods

Beautiful sea comes from a beautiful sky

Beautiful sea comes from a beautiful sky

Finally, we have reached the final section of our "Shrimp Ticket" iOS beginner series - the introduction and usage of CocoaPods.

Image

Without further ado, I will explain it in four steps according to the outline mentioned above.

What, Why, and How?#

What is CocoaPods❓

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


Why should we use it❓

Because it can automate, centralize, and intuitively manage third-party open-source libraries. It can be compared to Gradle, npm, and so on.

PS: The comparison of common iOS framework integration methods is as follows, referring 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 spec repository❗️ locally: pod setup, the spec repository is large, so grab a cup of coffee and wait~
  2. The usage steps are as follows:
    1. cd [project root directory]
    2. Initialize Pod: pod init, this will generate the Podfile.
    3. Edit the Podfile❗️
    4. Integrate the corresponding code library❗️ into the project: pod install
    5. To use it, simply #import <header file>.

If you are observant, you may have noticed the three ❗️ above. This is the focus of today🎺🎺🎺. Let's continue reading with the following two questions:

  1. How to edit the Podfile?
  2. What are spec repositories and code libraries, and what is the difference between them? You can temporarily understand them as instructions and physical objects.

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!  # Disable all warnings
use_frameworks!        # Use dynamic libraries, otherwise use static libraries

target 'MyApp' do                   # Specify 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 target for testing
    inherit! :search_paths         # Inherit the search paths from the parent target (at least including the above third-party libraries)
    pod 'OCMock', '~> 2.0.4'       # Import the latest version of OCMock >= 2.0.4 and < 2.1
  end
end
  • Pay attention to the version matching rules and the technique of nesting targets to reduce the repetition of writing pods. See the comments for more details.

Now that we have learned the simple method of writing the Podfile, let's see which third-party libraries are used in "Shrimp Ticket"👇

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 the local library by setting :path=>

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

Additional information🎺: You may wonder what ShoPiaoPiao is. It is a third-party library belonging to "Shrimp Ticket". Why is it imported locally using pod? In fact, I developed the "Shrimp Ticket" project as a third-party library. This is a common development method in the company, similar to SDK development. In the next section on common commands, I will mention how to create a third-party library based on CocoaPods.

Illustrated Principle#

To explain the basic principle of CocoaPods, I will use three images found online.

Let's answer the second question mentioned earlier: What are spec repositories and code libraries, and what is the difference between them?

Take a look at the images:

Find spec repositories and code libraries

  1. By installing the spec repository locally through pod setup, you are actually pulling the remote CocoaPods official index repository to your local machine. This index repository contains the description information of various third-party libraries, each stored in a spec file. The spec file contains information such as the library name, version number, description, source code address❗️, etc.

  2. When we have written the Podfile and run pod install, CocoaPods will use the source code address of each library to pull the source code and integrate it into the project. (Of course, this source code will be cached)

Now that you have seen the explanation, do you understand the difference between spec repositories and code libraries?


Let's look at two illustrations of the pod install process:

Process of pod install - 1

Process of pod install - 2

Follow the numbers and arrows to understand the process of pod install. If you understood the explanation above, understanding these two images should not be a problem~🤔

Common Commands#

I have compiled a chart of common commands that can be saved as a tool.

Common CocoaPods Commands


🥩Extrapod install VS. pod update

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

Similarities:

  • Both aim to fetch the code libraries required by the project.
  • Update the Podfile.lock.

Differences:

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

Detailed Explanation:

pod install

Check if the libraries in the Podfile are already included in Podfile.lock.

(1) If they are included, check if the versions are specified in the Podfile:

  1. If a version is specified, check if the version saved in Podfile.lock is the same as the one specified in the Podfile. If they are the same, skip it; if they are different, update it to the version specified in the Podfile.
  2. If no version is specified, do not check for updates and skip it.

(2) If they are not included, download the library and save the version in Podfile.lock.

PS: Podfile.lock is generally used when the Podfile does not specify versions.


pod update

Ignore the records in Podfile.lock, update the spec repository first, and then find the specified version of the dependency library in the Podfile. If no version is specified, it will find the latest version.

PS: pod update [PODNAME] can be used to specify a Pod library to update.

References#

What, Why, and How?

How do I install CocoaPods? - Stack Overflow

CocoaPods Guides - Official

Introduction to CocoaPods from the documentation - 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 about a month, I have completed the "Shrimp Ticket Getting Started with iOS" series. I hope you have gained some knowledge from it and can implement "Shrimp Ticket" on your own. Feel free to share your achievements~

In the future, I will organize a link to the source code of "Shrimp Ticket" and provide a summary✅.

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