One, foreword

In daily project development, common tool classes and common business modules need to be decoupled uniformly for development team projects of single product line or multiple product lines, so as to ensure the robustness and remolding of project structure. So for these modules, how to achieve better management? The most popular package management tool on iOS is Cocoapods.

This article describes how to implement toolclass private libraries, public libraries, and the Framework through Cocoapods.

Illustration of this article:

Conditions:

  • CocoaPods has been installed. If not, go to CocoaPods for installation

  • Making account

  • Yards cloud account

  • Code hosting platforms built by other platforms or internal private servers

Sign up for Cocoapods

In order to create an open source POD library, we need to register CocoaPods as a trunk library.

Pod Trunk Register Mailbox address'Username'--verbose For example, pod trunk register [email protected]'allenlas' --verbose
Copy the code

After registration, you will receive a confirmation email in your mailbox. Click the link in your browser to confirm your registration. After successful registration, you can execute the following command on your terminal:

Pod Trunk me // Check whether the registration is successfulCopy the code

To view the registration information, you can use the open source Pod library publishing tool in the future, or you can view previously released Pods in this way:

AllenLASdeMac-Pro:~ allenlas$ pod trunk me - Name: yourname - Email: [email protected] - Since: December 11th, 2017 02:29 - Pods: - XKCocoapods - Sessions: - December 11th, 2017 02:29 - April 24th, 2018 20:34. IP: 113.119.196.232 Description: macbook Mini AllenlasdeMac-Pro :~ Allenlas $Copy the code

Public and private libraries

Public and private library differentiation:
Creating a public Pod library or a private Pod library is actually based on the same git service and repo protocol. The difference is that the version index query is different between the two libraries. The public library podSpecs are managed by CocoaPods/Specs, while the internal private pod library needs to build its own repository to manage podSpecs.Copy the code
2.1 Set up public warehouse
Cocoapods is hosted on Github, and all Pods are hosted on Github, so this article is based on the Github public library. Github also supports free private repositories, which are used in the same way as the public libraryCopy the code
Create a warehouse

Details:
Repository name: name of the open source Repository (mandatory) 2. Description: Description of the functions and functions of the open source Repository 3. 3. Initialize this repository with a README Readme file, which is a complete description of the library. 6. Gitgnore: Select the type according to the situation 7.Copy the code
Add tag:
After the Repository is created, go to its home page, select Release, and add the Tag Repository home page -> Release -> Create tagCopy the code

As follows:

After all information is prepared, clone the project to the local PC on GitHub Desktop.

2.2 Automatically create projects based on Cocoapods
Command:
Pod lib create Project nameCopy the code

Under the clone project directory path, open the terminal and execute:

AllenLASdeMac-Pro:~ allenlas$ cd /Users/allenlas/Documents/GitHub/XKCocoapods
AllenLASdeMac-Pro:~ allenlas$ pod lib create XKCocoapods
Copy the code

The result is as follows:

Problem template details:
# Which platform do you want to use?1, What platformdo you want to use?? [ iOS / macOS ]
iOS
Library language selection?2, What languagedo you want to use?? [ Swift / ObjC ]
ObjC
Do you need a demo project to debug Pod?3, Would you like to include a demo application with your library? [ Yes / No ] YesWhich testing framework will you use?4, Which testing frameworks will you use? [ Specta / Kiwi / None ] NoneDo you want UI tests?5, Would you like todo view based testing? [ Yes / No ]
NO
Class name prefix?6, What is your class prefix? XKCopy the code
The project structure is as follows:

Of course, for the project structure contains one more layer of template files, based on the simplicity of the structure, you can raise all the files of the project to a level, directly in the root path of the local repository:

2.3 configuration podspec

Details:

Pod::Spec.new do |s|
  # library name
  s.name             = 'XKCocoapods'
  
  # Specify supported platforms and versions. Without write, all platforms are supported by default, and if multiple platforms are supported, use the following deployment_target definition
  spec.platform = :ios
  
  # version number
  s.version          = '1.0.0'
  
  # Library brief introduction
  s.summary          = 'iOS Public Library '
  
  # Open source library descriptionS. description = << -desc TODO: iOS public library makes DESC# Open source library address, or blog, social address, etc
  s.homepage         = 'https://github.com/ryanmans/XKCocoapods'
  
  # Open Source License
  s.license          = { :type= >'MIT', :file => 'LICENSE' }
  
  # Open source library author
  s.author           = { 'ALLen, LAS'= >'[email protected]' }
  
  Git tag = "git"; git tag = "git"
  s.source           = { :git => 'https://github.com/ryanmans/XKCocoapods.git', :tag => s.version }
  
  # Social urls
  s.social_media_url = 'https://juejin.cn'
  
  # Open source library minimum support
  s.ios.deployment_target = '8.0'
  
  Source library resource file
  s.source_files = 'XKCocoapods/Classes/**/*'
  
  Arc is supported
  s.requires_arc = true
  
  # Rely on system libraries
  s.frameworks = 'Foundation'
  
  # Open source libraries depend on libraries
  # s.divendency "navigation ", "~> 1"
  
  Add system dependency static library
  #s.library = 'sqlite3', 'xml2'
  
  # Add a framework that relies on third parties
  #s.vendored_frameworks = 'XXXX/XXXX/**/*.framework'
  
  # static library
  s.vendored_library = 'XXXX/XXX/XXX.a'.'YYY/YYY/Y.a'
  
  Add resource file
  #s.resource = 'XXX/XXXX/**/*.bundle'
  
  Add s.setatic_framework = true to your podSpec file and CocoaPods will configure the library as static framework. Support for both Swift and Objective-C
  #s.static_framework = true
end
Copy the code

The official information is as shown below:

Introducing utility classes
Go to the Classes file under 'XKCocoapods' in the root directory and put the XKCocoapods. H /m files you want to use thereCopy the code

And in the Example directory path, open terminal:

From the above information, you can see that the utility class and the libraries it depends on have been successfully added for use in the demo project.

After configuring the PodSpec and importing the library, you need to update the submission to Github, otherwise the last submission will be validatedCopy the code
2.4 verify podspec
Command:
1. pod lib lint --allow-warnings

2. pod spec lint --allow-warnings
Copy the code

The verification results are as shown in the figure:

Common errors may occur during validation:

- ERROR | [iOS] file patterns: The `source_files` pattern did not match any file
Copy the code

The reason for this error is that no matching file was found

Solutions:

1. Terminal execute the command: open/Users/allenlas/Library/Caches/CocoaPods/Pods/External/XKCocoaPods 2. Go to the nearest log file and place the utility Classes in the Classes directory where the source_files path is located: XKCocoapods -> Classes -> Add the XKCocoapods classCopy the code

As shown:

Update the submission code to re-verify the podSpec file’s viability.

2.5 Push podSpec to remote repository
Command:
pod trunk push xxxxx.podspec 
Copy the code

If you have not registered Cocoapods accounts, do as follows:

Pod Trunk Register Mailbox address'Username' --verbose

pod trunk me
Copy the code

The PodSpec is then pushed to the remote repository

As shown:

2.6 Checking whether the PodSpec is uploaded successfully
Command:
Pod Search XXXX (uploaded project name) for example: Pod Search XKCocoapodsCopy the code
Common mistakes:
[!]  Unable to find a pod with name, author, summary, or description matching `XKCocoapods`Copy the code

Solution:

Need to update the local repo to execute command pod setup or delete local search index rm ~ / Library/Caches/CocoaPods/search_index json, again pod search XKCocoapodsCopy the code

After the search is successful, the following figure is shown:

Make the framework

In the process of creating a public library, if you need to package the added utility classes into a working framework, you need to use the plug-in cocoapods-Packager

Installation command:
sudo gem install cocoapods-packager
Copy the code
3.1 Packing podSpec Files
Command:
Podspec --library --force package into a. A file. --force: indicates mandatory overwriting

Podspec --force package into a. Framework file
Copy the code
To configure the PodSpec file:
// Add the framework s.ios.vendored_frameworks = to be integrated'XKCocoapods/Classes/XKCocoapods.framework'
Copy the code

As shown:

Go to the root path of the project, open the terminal and run the following command:

AllenLASdeMac-Pro:~ allenlas$ cd /Users/allenlas/Desktop/XKCocoapods
AllenLASdeMac-Pro:~ allenlas$ pod package XKCocoapods.podspec
Copy the code

The execution result is shown in the following figure:

Once packaged, there will be an additional xkCocoapods-1.0.0 file locally

In its ios file, copy XKCocoapods. Framework, place it in Classes, and submit the update code

Note: Every time something changes, add a tag to Github and change the version number in your PodSpec to validate your PodSpecCopy the code

Re-validate podSpec