If you want to operate directly, please skip the preface and read the text directly. Suggest not to miss wonderful foreword oh, perhaps you read to you have inspiration.

preface

With the development of the project, version iteration and code reconstruction, there are generally many components with common business functions in our project, which may be used in many places in app or even in other projects. For example, there is a voice evaluation function in our FCS project, which is only needed in the FCS project in the early stage, and the same voice evaluation function is needed in reading picture books in a different project later. At this point, we generally have two approaches: one is to directly copy this part of the code into the picture book to read the project; The second is to extract the logic of voice evaluation and then use it in two places. Obviously at the beginning of the project, or the time urgent circumstances, the first way is very good to be able to do work, we also do so early, but I found that as the project goes on and the development, the time cost is too big, efficiency is very low, there are many hidden dangers (evaluation logic once there is a change, you need to change two times, And you can’t guarantee the same changes). The right thing to do is to immediately refactor the code at the appropriate time to extract this part of the duplicate code. The rule for refactoring is the Rule of three, which comes from Martin Fowler’s Refactoring improves the Design of Existing Code, a book that’s really recommended and one of the must-read books for programmers. Digress…

So I spent a few days extracting the logic for speech evaluation and building a public component that works with Speech Value Kit in both projects, and uses Cocoapods to manage it. The core code of the company project is stored in Coding, including the code for Speech ValuationKit, which we cannot disclose. Therefore, WHAT I have created are Private Libraries. Of course, there are public libraries, this part of the code can not only be used, but also open source.

The body of the

For details about how to install CocoaPods, see CocoaPods official documents. This document also describes how to use CocoaPods. The directory structure of the project is as follows:





  1. new[Project name].podSpec

    Use the following command:Pod Spec create 'project name',Pod lib create 'project name'.

    For an existing project, run the terminal command in the project root directory:pod spec create 'SpeechEvaluationKit', the root directory will generate aSpeechEvaluationKit.podspecFile.





    If the project has not already been createdpod lib create 'SpeechEvaluationKit'This step will ask you to fill in some information such as swift or OC, whether to use test framework, etc. My choices are as follows:





    The resulting directory is as follows,Add the local repository to the remote Git repository after writing the code for a new project, the association command isGit remote add Origin Specifies the remote repository address



  2. Configure the PodSpec file
Pod: : Spec. New do | s | s.n ame = 'SpeechEvaluationKit' s.v ersion = "1.0.0" s.s ummary = 'SpeechEvaluationKit is a powerful Voice evaluation library' s.description = 'A powerful voice evaluation library, such as book reading, living and ips mission.' s.homepage = 'https://git.coding.net/firstleap/SpeechEvaluation_iOS.git' s.license = { :type =>  'MIT', :file => 'LICENSE' } s.author = { 'XcqRomance' => '[email protected]' } s.source = { :git => 'https://git.coding.net/firstleap/SpeechEvaluation_iOS.git', : tag = > s.v ersion. To_s} s.i OS. Deployment_target = '8.0' s.s ource_files = 'SpeechEvaluationKit/Classes / / * * *' s.exclude_files = "SpeechEvaluationKit/Assets/**" endCopy the code

The information contained in this podSpec file is important enough for a regular PodSpec library to be readable for each configuration. For example, name stands for the name of your pods library. For public pods, you can use pod Search ‘name’. Version represents the version number, and changes the version number to publish the update as soon as it has been released. Summary and Description are brief and detailed descriptions of the Pods library, which are described according to the functionality of the library; The source code repository address can be git, SVN, or Github address. Source_files represents the code that your Pods library contains, which is usually the code we write for common components. Files are described using matching formulas such as ** and *. Exclude_files are usually resource files other than code, such as images, audio files, etc. As for other information, you can configure it according to your library requirements. For example, YTKNetwork is based on AFNetworking package, so it also adds s.networking “AFNetworking”, “~> 3.0” dependency. The above information must be carefully configured, otherwise there will be a warning or even an error failure in the next step of verification. For example, I did not change the summary but used the template information, and the following warning appears:





  1. Verify podSpec files and projects (pod lib lint Name.podspec)

    Execute the commandpod lib lint SpeechEvaluationKit.podspec(You can add parameters later--verboseTo help locate the problem that went wrong.)

    At this stage, there will be many warnings and errors that will cause the verification to fail. At this time, do not be impatient. Solve the problem one by onestack overflowLook for solutions. You’re dealing with a problem that many people have dealt with before.

    The following message is displayed if the verification succeeds:





    Once verified, you are one step away from success;

  2. Create tags and upload git repositories, tag versions and podspec filesversionKeep the version consistent;Git common command portal
Git push origin git push origin git push origin git push originCopy the code
  1. Publish the project to the Cocoapods official repository

    And so on… To do this, you need to create a Cocoapods account, which means you need to create a Cocoapods account by executing the following command:

    pod trunk register [email protected] 'romance' --description='My working computer'

    Your email address, name, and description will be your own, and you will receive an email, right click the link to activate, and then passpod trunk meTo check whether you have registered successfully:





    For multiplayer development, simply execute the following command:Pod trunk add-owner 'project name'Add a partner to the project;

    5.1 Publication of public libraries(Open source, podSpec files are uploaded to cocoapods, and when pod install or POD Update is executed, the corresponding source code is extracted from the configuration file.)

    command:Pod Trunk Push project name podSpec

    useName of the POD Search projectCheck whether the upload is successful. The following figure is the public library of log collection in our APPFLogKit.pod search FLogKit





    5.2 Publishing of private libraries

    command:Pod repo add 'storehouse'

    If it is an existing project, the code is placed in the coding private project and the repository address is filled directly into the PodSpec profile in step 2s.source“, that is, run the following command:pod repo add 'SpeechEvaluationKit' 'https://git.coding.net/firstleap/SpeechEvaluation_iOS.git';

    If it is a new project, as in Step 1, usePod lib create 'project name'To initialize the project, you need to establish a private repository in the server. Here I take the private repository in Coding as an example. After the repository is established, the git repository address will be obtained.





    Commit your code to the Coding Git repository and execute the publish commandpod repo add 'SpeechEvaluationKit' 'https://git.coding.net/romance/SpeechEvaluationKit.git';





    useopen ~/.cocoapods/reposCheck to see if the private repository is created and use it as soon as the code is updatedpod repo updateUpdate POD private repository, delete private repositoryPod repo remove 'project name'.

  2. Using the component

    pod 'SpeechEvaluationKit', :path => '.. / ', development mode

    pod 'SpeechEvaluationKit'After stabilization or other projects

conclusion

You’ll see how easy It is to manage common components in Cocoapods, although some of the more in-depth content will need to be understood in real life. There are only five steps to the process and its use: Create a PodSpec file -> configure the podSpec file -> validate the PodSpec file and project -> upload the tag-> Publish the update. Do you remember this step?

If you find this article helpful, please light up ❤️ at the bottom, your encouragement is my motivation!