[iOS] Cocoapods installation and use


foreword

Recently, the author used the api call data when imitating the weather forecast app. We can convert the general basic data types with the framework that comes with Xcode. But the format of the icon in the Zephyr weather api issvgFormat.
insert image description here

It seems that images in svg format can be directly used in Xcode after iOS13, but the author did not find the corresponding information. The author called a third-party library to use pictures in svg format. It took readers a day to install Cocoapods integrated with the third-party library and use it. I hereby write a blog record. If there are any deficiencies, please feel free to point out

1. The role of Cocoapods

Dependency management: CocoaPods allows developers to define the required third-party libraries and frameworks in the project, and then automatically download, install and configure these dependencies. This simplifies the project setup and update process by eliminating the need for developers to manually manage these libraries.

Library integration: Through CocoaPods, developers can easily integrate various powerful third-party libraries, which can implement various functions, such as network requests, image loading, database operations, UI controls, etc. These libraries are extensively tested and optimized to help speed up the development process and improve application quality.

Version management: CocoaPods allows developers to specify a specific version or range of required libraries, ensuring that the version of the library used in the project is controllable. This avoids potential compatibility issues and allows more flexibility to control version upgrades when the library needs to be updated.

Quick integration: With CocoaPods, developers can quickly integrate new third-party libraries by simply adding the name and version requirements of the library to the project configuration file, and then running a command to install it.

Support for private libraries: In addition to public libraries, CocoaPods also supports the integration of private libraries. This is useful for shared code developed within a team or non-public libraries obtained from third parties.

Update management: CocoaPods can regularly check and update the third-party libraries used in the project, so that developers can obtain the latest functions and fix bugs in time, and maintain the stability and security of the project.

Simply put, Cocoapods is a tool that can regularly update third-party libraries in the project, making it easier for users to use third-party libraries.


2. Install Cocoapods

Before the installation, I need to say something more, because our Cocoapods is written in Ruby language, so we need to download related software about Ruby language before downloading Cocoapods

There are many tutorials on installing Cocoapods on the Internet, most of which were done a few years ago, and some methods may not be suitable for the current Mac. Here is the blog I refer to. Follow this blog to have a high probability of successfully installing Cocoapods .


Here you need to pay attention to the rvm, ruby, and RubyGems used when installing Cocoapods. It is best to upgrade to the latest version. With the update of the Mac, the version of ruby, etc. required by CocoaPods is getting higher and higher. For example:

ERROR:  Error installing cocoapods:
	The last version of activesupport (>= 5.0, < 8) to support your Ruby & RubyGems was 6.1.7.4. Try installing it with `gem install activesupport -v 6.1.7.4` and then running the current command again
	activesupport requires Ruby version >= 2.7.0. The current ruby version is 2.6.10.210.

This is an error encountered by readers during installation. This error message indicates that the currently installed Ruby version does not meet the requirements of CocoaPods. The minimum Ruby version required for CocoaPods is 2.7.0, and my Ruby version was 2.6.10.210.

In fact, there are not many Ruby versions that we can download through the terminal:
insert image description here
Therefore, the author recommends that all the software that needs to be installed with Cocoapods be installed to the latest version from the beginning.

3. Using Cocoapods

After installing Cocoapods, how do we call the third-party library? When I first started using it, the author also encountered many problems, here is a summary of the author's use method


  1. Find the project folder that needs to use the third-party library.
    insert image description here
    There is one thing to note here.The name of the folder is the name of our project, which will be used later

  1. Locate our engineering location at the terminal

The method here is toEnter cd + space in the terminal, and then drag our project folder into the terminal, so that the terminal successfully indexed our project

cd 工程名

insert image description here


  1. Create a podfile in the project folder

Enter the following command in the terminal

touch podfile

insert image description here
You can see that a podfile is generated in our project

Podfile role: specify the third-party libraries required by the project and their version requirements


  1. Open the podfile and edit it

Here is an example of installing the third-party library of AFNetworking

platform:ios,'9.0'

target '工程名' do
 
pod 'AFNetworking' #这里也可以是其他的第三方库

end

insert image description here


  1. Install our third-party library in the terminal

Enter the command in the terminal pod install
insert image description here
After the installation is complete, you can see that the AFNetworking library has appeared in our project


  1. Open the original project file for compilation

Open the original project for compilation operation
insert image description here
. This must be remembered.After successfully installing our third-party library, we must open the file to compile and run(command + r or command + b is fine), and then you will see an error:
insert image description here
this is certain, and then we close the file


  1. Open the newly created .xcworkspace for follow-up work.
    insert image description here
    We compiled the .xcworkspace and found that it can be compiled successfully, and then we click on our original project file, and a prompt will appear: that is to say, these insert image description here
    two files we Only one can be opened, but the original project file cannot be linked with the third-party library we installed, so our workbench becomes the newly created .xcworkspace file,Similarly, our code needs to be written in the .xcworkspace file in order to compile and run

The sixth and seventh steps are the most important steps in the operation. I have been troubled by these steps for a long time before. I don’t understand the operating principles of the sixth and seventh steps, but it should probably make our project and the first step The operation of linking with the third-party library

Summarize

So far, the installation and use of Cocoapods has probably been talked about. This blog is only for communication and learning. If there are any deficiencies or mistakes, please feel free to point out

Guess you like

Origin blog.csdn.net/weixin_72437555/article/details/131950316