With the continuous increase of project functions, more and more developers join the business line, resulting in a series of problems such as more and more serious coupling, slower and slower compilation, and independent testing. To address such situations, consider using componentized development

  • concept

Componentization is the decomposition of a single project into its individual components and the organization of any project with complete business logic in a certain way.

  • advantage

    • Independent: Independently write, compile, run, and test
    • Reuse: Reuse of functional code. For example, different projects use the same functional module
    • Efficient: add and delete modules at will to achieve efficient iteration
    • Componentization can also work with binarization to speed up project compilation
  • Component classification

There are three broad categories: base components, functional components, and business components

  • Basic components: also known as common components, which hold defined macros, constants, protocols, categories, encapsulation classes for necessary third parties, and various processing tools such as time, date, device information, file processing, sandbox management, etc
  • Functional components: custom view controls, encapsulation of some specific functions (such as recording, playback audio encapsulation)
  • Business components: lines of business

This article first introduces the basic use of remote private library, it is recommended that after reading the sequence, come back to look at the steps again, deepen understanding, if there are shortcomings, welcome to point out, thank you:)

Steps of induction

  1. Create remote index libraries and private libraries

  2. Add remote index library to local Pod Repo add index library name index library address

  3. Create a pod template library locally. Pod Lib Create Component name Add the core code of the framework to the Classes directory. Local install tests whether the core code is available

  4. Git push origin master git tag ‘0.1.0’ git push origin master git tag ‘0.1.0’ git push –tags

  5. Pod Lib Lint –private Pod Spec Lint — Private Pod Repo push The local name of the index library xx.podspec

  6. Use source official index url Source private index url pod ‘component name’ POD install

How do we create and use private libraries

Create a private index library

Using the code cloud as an example, create a private index library for LXFSpecs, which acts as its name indicates, for indexing

Add a private index library locally

1. View the local index library

pod repo
Copy the code

As you can see, the only public index on Github is currently locally available

2. Add a private index library

Add our newly created private index library LXFSpecs to the local

/ / pod repo add index library name Index base address pod repo add LXFSpecs https://gitee.com/LinXunFeng/LXFSpecs.gitCopy the code

Now that there are two local index libraries, let’s forget about the index libraries

Create component libraries

The operation on the code cloud is the same as above. Here, LXFBase is used as an example to create the base component library

1, quickly create template library

Go to the appropriate location and create a folder with the same name as the component. Once the CD is inside, use the following command

// pod lib create component name pod lib create LXFBaseCopy the code

This will let you configure some information, according to your own situation can be configured.

2. Add component content

After the creation is complete, it will automatically open the corresponding Example project for us, and the files like the one shown in the LXFBase directory will appear. We will throw the basic components into the Classes folder and delete the replaceme. m file

The default Classes folder is where the files are to be downloaded when pod Install is installed, although you can change the location by modifying the configuration of the Spec file

Install and test the local library

You can see this in the Podfile file of the Example project

pod 'LXFBase', :path => '.. / 'Copy the code

The template library has already specified the location of LXFBase. Podspec in the Podfile by default, making the LXFBase component easy to install and test

pod install
Copy the code

As you can see, we have added the local component to Example. Now feel free to do all the tests you want to make sure the component works.

Once we’ve tested the component, we’ll upload the PodSpec file to the private index library, but before we can do that, we need to modify the spec.

4. Modify the Spec

Specific configuration instructions can refer to Cocoapods to create a third-party framework

Major modifications

S.name = 'LXFBase' s.sion = '0.1.0' s.summary = 'LXFBase.' s.description = << -desc Including classification and common tools DESC Sheldon horowitz omepage = 'https://gitee.com/LinXunFeng/LXFBase' s.s ource = {: git = > 'https://gitee.com/LinXunFeng/LXFBase.git', :tag => s.version.to_s } s.source_files = 'LXFBase/Classes/**/*'Copy the code

Upload the component code

1. Commit code to the component repository

git add . git commit -m 'firstCommit' git remote add origin https://gitee.com/LinXunFeng/LXFBase.git // // git push -f origin master git push origin masterCopy the code

2. Label

Label 0.1.0 is consistent with S. sion in spec

Git tag '0.1.0' git pushCopy the code

Submit a PodSpec to a private index library

We can save time by verifying the spec file before we upload it, otherwise it will be annoying to upload it for a long time and still fail to verify

1. Validates the required fields of the Spec locally

// Local validation does not validate the tag pod lib lint in s.ourceCopy the code

2. Remote verification

// Remote validation validates the tag at s.ource and reports an error pod Spec Lint if it is not markedCopy the code

If you try to authenticate remotely without tagging it and uploading it to a remote private library, you’re going to get an error

After typing and uploading the tag, the remote verification is successful, and we can proceed to the next step: submit the PodSpec file to the index library

  • Verify private library prompts

If you are verifying a private library, add –private to the end. Otherwise you will get a warning, which you can ignore by selecting –allow-warnings

pod lib lint --private
pod spec lint --private
Copy the code

Submit a PodSpec

// Podspec Pod repo push LXFSpecs LXFBase. PodspecCopy the code

Push our code directly to the local index library LXFSpecs. After push, it automatically synchronizes our code to the remote index library

Take a look at the private index library LXFSpecs on the code cloud

Let’s test search our component

pod search 'LXFBase'
Copy the code

Use private libraries

At this point we can try adding the component LXFBase in pod form to create a new project

1. Add the Podfile file

pod init
Copy the code

2. Add the following description at the top of your Podfile

/ / the second line is in order to guarantee the normal use of the public library source 'https://gitee.com/LinXunFeng/LXFSpecs.git' source 'https://github.com/CocoaPods/Specs.git'Copy the code

3. Add the LXFBase component

pod 'LXFBase'
Copy the code

4. Install components

pod install
Copy the code