English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Advanced Part of Detailed Explanation of CocoaPods in iOS

I. Podfile.lock file
As mentioned above, after executing pod install and starting to use CocoaPods, a Podfile.lock file will be generated. This file may seem unrelated to us, but in fact, it should not be ignored at all.
This file is used to save the versions of installed Pods dependency libraries. After installing the three Pods dependencies SBJson, AFNetworking, and Reachability through CocoaPods, the content of the corresponding Podfile.lock file is as follows:

PODS:
 - AFNetworking (21.0):
  - AFNetworking/NSURLConnection
  - AFNetworking/NSURLSession
  - AFNetworking/Reachability
  - AFNetworking/Security
  - AFNetworking/Serialization
  - AFNetworking/UIKit
 - AFNetworking/NSURLConnection (21.0):
  - AFNetworking/Reachability
  - AFNetworking/Security
  - AFNetworking/Serialization
 - AFNetworking/NSURLSession (21.0):
  - AFNetworking/NSURLConnection
 - AFNetworking/Reachability (21.0)
 - AFNetworking/Security (21.0)
 - AFNetworking/Serialization (21.0)
 - AFNetworking/UIKit (21.0):
  - AFNetworking/NSURLConnection
 - Reachability (3.0.0)
 - SBJson (4.0.0)
DEPENDENCIES:
 - AFNetworking (~> 2.0)
 - Reachability (~> 3.0.0)
 - SBJson (~> 4.0.0)
SPEC CHECKSUMS:
 AFNetworking: c7d7901a83f631414c7eda1737261f696101a5cd
 Reachability: 500bd76bf6cd8ff2c6fb715fc5f44ef6e4c024f2
 SBJson: f3c686806e8e36ab89e020189ac582ba26ec4220
COCOAPODS: 0.29.0

The greatest use of the Podfile.lock file is for multi-person development. For the writing style of not specifying the version of the Pods dependency library in the Podfile, as follows:

pod 'SBJson'

This sentence is used to obtain the latest version of the SBJson Pods dependency library.
After someone in the team executes the pod install command, the Podfile.lock file records the latest version of the Pods dependency library at that time. When other members of the team check out this project that includes the Podfile.lock file, and then execute the pod install command, the version of the Pods dependency library obtained will be consistent with the version initially obtained by the user. Without the Podfile.lock file, all subsequent users executing the pod install command will obtain the latest version of SBJson, which may cause the same team to use inconsistent versions of the dependency library, which is absolutely a disaster for team collaboration!
In this case, if the team wants to use the latest version of the SBJson dependency library, there are two options:
Modify the Podfile to point to the latest version of the SBJson dependency library;
Execute the pod update command;
Given the importance of the Podfile.lock file for team collaboration, we need to add it to version control.

Second, Podfile file
For ordinary users, the most frequently dealt with file when using CocoaPods is the Podfile. CocoaPods is implemented in Ruby, so the syntax of the Podfile is Ruby syntax. The following aspects will be introduced from the Podfile:
1Podfile file storage location
This is a problem left over from the previous article. Usually, we recommend placing the Podfile file in the root directory of the project, as shown in the following figure:

In fact, the Podfile file can be placed in any directory. What needs to be done is to specify the project path in the Podfile. Compared to the original, the Podfile file is added at the very beginning with a line as follows:

xcodeproj \/Users/wangzz/Desktop/CocoaPodsTest/CocoaPodsTest.xcodeproj
platform :ios 
pod 'Reachability', '~> 3.0.0' 
pod 'SBJson', '~> 4.0.0' 
platform :ios, '7.0' 
pod 'AFNetworking', '~> 2.0' 

The specified path uses the xcodeproj keyword.
After that, enter the path of the Podfile file, and execute the pod install command, just like before, these Pods dependency libraries will be downloaded, and the generated related files are all placed in the directory below the Podfile, as shown in the figure below:

As before, we still need to use the generated workspace file to open the project.

2、Podfile and target
The Podfile is essentially used to describe the targets in Xcode projects. If we do not explicitly specify the target corresponding to the Podfile, CocoaPods will create an implicit target named default, which corresponds to the first target in our project. In other words, if there is no target specified in the Podfile, only the first target in the project can use the Pods dependency libraries described in the Podfile.
If you want to describe multiple targets in a Podfile at the same time, depending on the different requirements, there can be different implementation methods. To illustrate the problem, create a target named Second in the original project, and now the project contains the following targets:

① Multiple targets use the same Pods dependency libraries
For example, if both the target named CocoaPodsTest and the target named Second need to use the three Pods dependency libraries Reachability, SBJson, and AFNetworking, you can use the link_with keyword to implement it, and write the Podfile in the following way:

link_with 'CocoaPodsTest', 'Second'
platform :ios 
pod 'Reachability', '~> 3.0.0' 
pod 'SBJson', '~> 4.0.0' 
platform :ios, '7.0' 
pod 'AFNetworking', '~> 2.0' 

This writing style realizes that the two targets CocoaPodsTest and Second share the same Pods dependency libraries.
② Different targets use completely different Pods dependency libraries
CocoaPodsTest this target uses the three dependency libraries Reachability, SBJson, and AFNetworking, but the Second target only needs to use the OpenUDID dependency library. In this case, you can use the target keyword, and the Podfile description is as follows:

target :'CocoaPodsTest' do
platform :ios 
pod 'Reachability', '~> 3.0.0' 
pod 'SBJson', '~> 4.0.0' 
platform :ios, '7.0' 
pod 'AFNetworking', '~> 2.0'
end
target :'Second' do
pod 'OpenUDID', '~> 1.0.0'
end

Among them, do/end as the start and end identifier.
3Using Podfile to manage Pods dependency library versions
When introducing a dependency library again, it is necessary to explicitly or implicitly indicate the version of the referenced dependency library, and the specific writing and meaning are as follows:

pod 'AFNetworking'   //Not explicitly specifying the version of the dependency library means that the latest version will be fetched each time.
pod 'AFNetworking', ''2.0'   //only use2version
pod 'AFNetworking', '>' 2.0'   //use higher than2.0 version
pod 'AFNetworking', '>=' 2.0'   //use greater than or equal to2.0 version
pod 'AFNetworking', '<' 2.0'   //use less than2.0 version
pod 'AFNetworking', '<=' 2.0'   //use less than or equal to2.0 version
pod 'AFNetworking', '~> 0.'12   //use greater than or equal to 0.12but less than 0.2version
pod 'AFNetworking', '~>0.'1   //use greater than or equal to 0.1but less than1.0 version
pod 'AFNetworking', '~>0'   //For versions higher than 0, writing this restriction is the same as not writing anything. Both indicate that the latest version is used.

III. Common CocoaPods Commands
1pod install
Install the dependency library according to the content specified in the Podfile file. If there is a Podfile.lock file and the corresponding Podfile file has not been modified, it will install according to the version specified in the Podfile.lock file.
Each time the Podfile file is updated, the command needs to be executed again to reinstall the Pods dependency libraries.
2pod update
If the version of the dependency library specified in the Podfile is not fixed, when the corresponding dependency library is updated, it will always fetch the latest version of the dependency library allowed by the Podfile description, regardless of whether there is a Podfile.lock file.
3、pod search
The command format is:

$ pod search OpenUDID

The following OpenUDID is the parameter.
It is not difficult to see from the name of the command that this command is used to search for available Pods dependency libraries by name, and the execution result is as follows:

-> OpenUDID (1.0.0)
  Open source initiative for a universal and persistent UDID solution for iOS.
  pod 'OpenUDID', '~> 1.0.0'
  - Homepage: http://OpenUDID.org
  - Source:  https://github.com/ylechelle/OpenUDID.git
  - Versions: 1.0.0 [master repo]

Here we found a piece of available data, which describes the brief information of the OpenUDID library. In fact, what we really need is the third line of the above result:

pod 'OpenUDID', '~> 1.0.0'

It is not difficult to see that this is what we need to add to the Podfile file.
With this command, it can be convenient and quick to find the required Pods dependency libraries.
4、pod setup
The command format is:

$ pod setup

After execution, the following will be printed:

Setting up CocoaPods master repo
Updating 7cd4668..f3d3ced
Fast-forward

Next, many update information will be printed.
This command is used to update the local computer's saved Pods dependency library tree. Since many people create or update Pods dependency libraries every day, this command can be quite slow, please be patient. We need to execute this command frequently, otherwise, when there are new Pods dependency libraries, the pod search command will not be able to search for them.

Chapter 4: Reference Documents

http://guides.cocoapods.org/using/index.html

You May Also Like