Recently I sorted out the project and found that I used a lot of third-party SDKS. These SDKS were directly dragged into the project and were in a mess. I plan to give all these third-party SDKS to CocoaPods to manage.

What is CocoaPods?



cocoapods.org

CocoaPods creates a new local repository

Here I use wechat SDK for example, from the wechat developer Center to download the corresponding SDK.

We get a WechatSDK folder, and the content inside is also relatively simple

└ ─ ─ WechatSDK ├ ─ ─ libWeChatSDK. A ├ ─ ─ the README. TXT ├ ─ ─ WechatAuthSDK. H ├ ─ ─ WXApi. H └ ─ ─ WXApiObject. HCopy the code

To get CocoaPods to manage third-party SDKS, we need to make the third-party SDKS into a repository, just like we would with podfiles. We are going to create a.podspec file in WechatSDK. This file is used to describe the current repository. Guides.cocoapods.org/syntax/pods…

Credit: LeonLei’s blog http://www.gaoshilei.com

Create a new wechatSDK. podSpec and fill it in with the following information:

# coding: utf-8
Pod::Spec.new do |s|
  s.name         = "WechatSDK" The name of the repository used in Podfile, similar to AFNetworking

  s.version      = "1.8.2" # Mandatory, version number

  s.summary      = "WeChat SDK" # Write anything

  s.description  = <<-DESC # Write anything
                    WechatSDK repo
                     DESC

  s.homepage     = "http://git.internal.weyao.com/gaoshilei/WechatSDK" # Write anything
  s.license    =  'MIT' # Write anything
  s.authors      = { "gaoshilei"= >"[email protected]" } # Write anything
  s.platform     = :ios # required
  s.ios.deployment_target = '8.0' # required
  s.source =  { :git= >'./Vendors/WechatSDK'.:tag= >"1.8.2" } This is the path of your repository relative to your Podfile.
  s.source_files = '*.{h,m,mm,c}' # Mandatory, code file. If you have a secondary directory, include all the code files in the secondary directory, denoted by **. It is also described in the link given above
  # s.frameworks = 'QuartzCore', 'CoreData
  s.vendored_libraries = 'libWeChatSDK.a' # This is mandatory if a third-party SDK has a lib package (note the path)
  # # s.v endored_frameworks = 'ThirdPartyFramework. Framework' third party SDK if there is a framework, need to add here (note that path)
  s.requires_arc = true Unless you are still writing MRC code

end

Copy the code

The s.ource key needs to be explained in detail. This is the location of the Git repository that CocoaPods needs to read from. This is the location of the git repository that CocoaPods needs to create in the WechatSDK folder. And then the tag fits. Execute the following command in the current directory:

git init 
git add.
git commit -m "commit"Git tag 1.8.2Copy the code

Note that the path to the Git repository address should be set relative to the path to your project Podfile, so my project Podfile is Vendors, so I should write./Vendors/WechatSDK. Now that you’re more than halfway there, it’s time to configure it correctly in your project’s Podfile

  pod 'AFNetworking'. pod'WechatSDK'.:podspec= >'./Vendors/WechatSDK/WechatSDK.podspec'.Copy the code

Specify the path to the repository configuration file, again relative to the Podfile. Then execute pod install –no-repo-update installation in the project, which generally has the following problems (which I encountered during production)

The first case:

[!]  Failed to load'WechatSDK' podspec:[!]  Invalid`WechatSDK.podspec` file: undefined local variable or method `tag` for Pod:Module
Copy the code

In this case, CocoaPods can’t find the repository’s PodSpec configuration file because the path in the Podfile is incorrectly written. Instead, CocoaPods can change the repository’s path relative to the Podfile.

The second case:

[!]  Error installing WechatSDK [!]  /usr/bin/git clone $HOME/Desktop/LittleBee/LittleBee_iOS/Vendors/WechatSDK /var/folders/6y/w9bcrs915g17gt732n84x6p40000gn/T/d20180119-75062-1jdxrw8 --template= --single-branch --depth 1 --branch 1.8.2

fatal: repository '$HOME/Desktop/LittleBee/LittleBee_iOS/Vendors/WechatSDK' does not exist
Copy the code

This is obvious because the s.ource path in wechatsdK. podspec is an absolute path, and the solution is to change it to a relative path.

The third case:

[!]  The`WechatSDK` pod failed to validate due to 1 error:
    - ERROR | [iOS] File Patterns: File patterns must be relative and cannot start with a slash (source_files).
Copy the code

In this case, the PodSpec configuration file is not formatted properly, so you can modify it by referring to the documentation on the official website.

You can also make your own code into a library. Change s.source to github address to create a shareable online CocoaPods library similar to AFNetworking. One more step is to push your own open source library configuration file into CocoaPods’ PodSpec repository (I haven’t done this yet, but I’ll leave it to Google or Google).

Create a private central repository

It’s all done. It looks perfect, but it still has its flaws. It is obviously not the same as the pod format we usually use, and it is too troublesome to configure the path.

  pod 'AFNetworking'
  pod 'WechatSDK'.Copy the code

Wouldn’t that be perfect! CocoaPods actually manages all of its repositories through PodSpec and has a directory locally

~/.cocoapods/repos
Copy the code

CocoaPods puts the central repository in this location, so when we execute pod install, we will go to the central repository to check whether there is the target open source component, and then git clone it, so sometimes there is the open source component. However, when we execute pod install, there is always a message indicating installation failure. There is no such directory. That’s because the local repos are too old, so a POD repo update is enough. Based on CocoaPods, we can implement the above method.

How does CocoaPods’ central repository manage these open source plug-ins

The path looks something like this

LeonLeiMBP15-145:AFNetworking gaoshilei$ pwd
/Users/gaoshilei/.cocoapods/repos/master/Specs/a/7/5/AFNetworking
LeonLeiMBP15-145:AFNetworking gaoshilei$ cd 3.1.0/
LeonLeiMBP15-145:3.1.0 gaoshilei$ ls -o
total 8
-rw-r--r--  1 gaoshilei  2889 12 19  2016 AFNetworking.podspec.json
Copy the code

AFNetworking has a number of version number folders, each corresponding to the repository configuration file. We could create our own private central repository. Find a directory and build your own private central repository

LeonLeiMBP15-145:~ gaoshilei$ cd Desktop/
LeonLeiMBP15-145:Desktop gaoshilei$ mkdir CocopodsPrivate
LeonLeiMBP15-145:Desktop gaoshilei$ cd CocopodsPrivate/
LeonLeiMBP15-145:CocopodsPrivate gaoshilei$ mkdir WechatSDK
LeonLeiMBP15-145:CocopodsPrivate gaoshilei$ cd WechatSDK/
LeonLeiMBP15-145:WechatSDK gaoshilei$ mkdir 1.8.2
LeonLeiMBP15-145:WechatSDK gaoshilei$ cd 1.8.2/
LeonLeiMBP15-145:1.8.2 gaoshilei$ cp ~/Desktop/LittleBee/LittleBee_iOS/Vendors/WechatSDK/WechatSDK.podspec .
Copy the code

CocoaPods needs to be aware of the repository’s existence. This repository needs to be shared with the company git for team development purposes:

LeonLeiMBP15-145:CocopodsPrivate gaoshilei$ git init Initialized empty Git repository in /Users/gaoshilei/Desktop/CocopodsPrivate/.git/ LeonLeiMBP15-145:CocopodsPrivate gaoshilei$ git add . LeonLeiMBP15-145:CocopodsPrivate gaoshilei$ git commit -m "first commit" [master (root-commit) aa27f56] first commit 1 file changed, 25 insertions (+) create mode 100644 WechatSDK / 1.8.2 / WechatSDK podspec LeonLeiMBP15-145: CocopodsPrivate gaoshilei $git remote add origin [email protected]:gaoshilei/CocoapodsPrivate.git LeonLeiMBP15-145:CocopodsPrivate gaoshilei$ git push origin masterCopy the code

This private central repository is then added to CocoaPods

LeonLeiMBP15-145:CocopodsPrivate gaoshilei$ pod repo add CocoapodsPrivate [email protected]:gaoshilei/CocoapodsPrivate.git
Copy the code

After the pod search WechatSDK is successfully added, you can see that the version number is from the repository CocoapodsPrivate

Let the other team members perform pod repo add CocoapodsPrivate [email protected]: gaoshilei/CocoapodsPrivate git, you can use this private center warehouse.

[!] when you can find WechatSDK but cannot execute pod install, you will get this error: [!] Unable to find a specification for ‘WechatSDK’ because we need to add the corresponding repository address to the project’s Podfile:

platform :ios, '8.0' source 'https://github.com/CocoaPods/Specs.git' source '[email protected]: gaoshilei/CocoapodsPrivate git' . pod 'WechatSDK' ......Copy the code

Add these two and then execute Pod Install.