Install and use CocoaPods for iOS development and cocoapods for ios development
Preface
Note: The content of this article is excerpted from the http://blog.csdn.net/lizhongfu2013/article/details/26384029 content made some modifications and updates.
I. Summary
During iOS development, many third-party libraries are referenced in the project, and CocoaPods (https://github.com/CocoaPods/CocoaPods) can be used to conveniently manage these third-party libraries in a unified manner.
Ii. Installation
Since the tutorials on the Internet are basically the same, but the details are not perfect, I would like to take the opportunity to add them here:
Note: To use CocoaPods, You need to download and install CocoaPods. to download and install CocoaPods, you need the Ruby environment.
1. Ruby Environment Construction
The current installation environment is Mac mini 10.10.3. The Mac OS itself comes with Ruby, but I still need to update the insurance, because the first installation failed without updating Ruby.
1-1 check the current ruby version: Open the terminal and enter ruby-v (it is installed, but this version fails to work next, so update ruby)
1-2 update ruby
Enter the following command on the terminal (point the Ruby image to taobao to avoid being attacked)
1. gem sources -- remove https://rubygems.org/(this address is walled)
2. gem sources-a http://ruby.taobao.org/
3. gem sources-l (used to check whether the image is successfully replaced)
2. Download and install CocoaPods
Terminal input: sudo gem install cocoapods
The above information is displayed, indicating that the installation is successful. 3. Use CocoaPods
3-1 create a project named PodTest
3-2 in the terminal, cd to the project directory (note: the directory containing the PodTest folder, PodTest. xcodeproj, and PodTestTest)
Example: cd/Users/KingKong/Desktop/PodTest
3-3 create a Podfile (configuration file)
Enter touch Podfile
Next, enter vim Podfile on the terminal.
Enter I on the keyboard to enter the editing mode. Enter
Platform: ios, '7. 0'
Pod 'mbprogresshud ',' ~> 0.8'
Then Press Esc and enter ":" to enter the vim command mode, and then enter wq behind the colon
Note: You can enter wq only after entering: on the keyboard. Press enter and you will find one more Podfile file in the total directory of the PodTest project.
Exciting time: confirm the terminal cd to the project directory, then enter pod install, wait for a while, about 3 minutes.
View the project root directory:
Note: To open a project, click PodTest. xodeproj instead of PodTest. xcworkspace.
There are several notes for engineering changes:
A. Third-party libraries will be introduced to the project as static libraries.
CocoaPods organizes all third-party libraries into a project named Pods as target, which is placed in the newly generated Pods directory. The entire third-party library project will generate
A static library named libPods. a is provided to the project.
B. The role of xcworkspace (PodTest. xcworkspace) is to manage projects and third-party libraries.
PodTest and Pods projects are organized and managed as workspace.
Open the project and check the project structure:
Running result:
4. Supplement: 1. The basic installation and use of CocoaPods are described in detail, but there are some supplements. What should I do when multiple third parties need to be imported at the same time?
In this case, you need to modify the Podfile, which is the file saved in the project root directory edited by vim. After modifying the Podfile file, you need to re-execute the pod install command.
For example:
Platform: ios
Pod 'jsonkit', '~> 1.4'
Pod 'afnetworking', '~> 2.0'
2. CocoaPods can find the third-party library you want
Terminal input command: pod search UI
Crazy! How can I find such popular keywords? Many databases ~~
Then re-edit the Podfile file and follow the previous steps to import more databases to the project!
3. In the previous step, "Create a Podfile (configuration file) and enter vim Podfile on the terminal. If you are not used to using the vim editor, you can also use the linux Command touch, generate an empty Podfile and use other text editors. For example:
1 KingKong-Pro:~ kingkong$ cd /Users/lizhongfu/Desktop/TestProject 2 KingKong-Pro:TestProject kingkong$ touch Podfile
After editing the Podfile file, you can execute the pod install third-party import (for details, see add 1st). CocoaPods starts to download the source code, configure dependencies,
Introduce the required framework and other columns.
4. If a project managed by CocoaPods has been generated, You can edit Podfile in Xcode. After editing and saving, do not forget to cd to the root directory and call the pod install command.
5. When a project has multiple targets, you must configure the Podfile file to support the newly added Target. Otherwise, only the Target generated when the project is created by default is supported:
A. If you create a new Target named Second, and the third-party support required by Second and Test is the same, that is, using the same Pods dependency library, you can use
Link_with Keyword:
1 link_with 'Test', 'Second' 2 platform :ios 3 platform :ios, ‘9.0’ 4 pod 'AFNetworking', '~> 2.0'
B. If different targets require different dependent libraries, you can
1 platform :ios 2 target :'Test' do 3 pod 'Reachability' 4 pod 'SBJson' 5 pod 'AFNetworking' 6 end 7 8 target :'Second' do 9 pod 'OpenUDID' 10 end
6./Library/Ruby/Gems/2.0.0/gems/claide-0.8.1/lib/claide/command. rb: 417: in 'Help! ': [!] You cannot run CocoaPods as root. (CLAide: Help)
Re-open a terminal, and then pod install, do not sudo pod install.
7. If $ pod install encounters:
Analyzing dependencies
Fetching podspec for 'uaappreviewmanager 'from '..'
[!] Unable to satisfy the following requirements:
-'Uaappreviewmanager (from '...') 'required by 'podfile'
8. To remove Cocoapods, you can:
A. Delete the Podfile, Podfile. lock, and Pods folders in the project folder.
B. Delete the xcworkspace File
C. Use the xcodeproj file to open the project and delete the Pods. xcconfig and libPods. a references in the Frameworks group.
D. Delete Check Pods Manifest. lock and Copy Pods Resources under Build Phases in the Project Settings.
Ps: If cocoapods is integrated into the project and its related files are accidentally modified or deleted, it will fail. For example: accidentally
After Pods. xcconfig is deleted, the following error occurs: diff:/../Podfile. lock: No such file or directory. After cocoapods is deleted,
Run $ sudo pod install again.
If permission issues occur during compilation, perform the following operations on the project folder $ sudo chmod 777 path-to-project-folder /*
$ Sudo chown 777 path-to-project-folder /*
9. When the database is imported normally but the relevant header file still cannot be found, you can:
In TARGETS-> Search Paths-> User Header Search Paths, write $ {SRCROOT} and change the following parameter to recursive:
10. Xcode-ld: library not found for-lPods
After a new Target is added and the pod is installed, the old Phases are referenced in the Build Phases of the Target created by default. a, such as libPods. a. When a new Targget is added,
LibPods. a has become a libPods-Test.a, and the newly added Target name is Second, dependent. a is the libPods-Second.a. Therefore, libPods. a is no longer referenced and not generated,
If any reference is made, an error is reported. The solution is to delete useless. a references from Build Phases of the Target, such as libPods..
11. pod update
If the specified dependency library version in Podfile is not writable, no matter whether there is any Podfile when the corresponding dependency library is updated. the lock file will get the latest dependent library version that can be obtained according to the description of the Podfile file.
12. If you want to clarify the version number of a third party when referencing a third party, there are some minor details:
1 pod 'afnetworking' // If the dependent library version is not explicitly specified, the latest version 2 pod 'afnetworking', '2. 0' // use only version 2.0 3 pod 'afnetworking', '> 100' // use version 4 pod 'afnetworking' higher than Version 2.0 ', '> = 100' // use version 5 pods later than or equal to 2.0 'afnetworking',' <100' // use version 6 pods earlier than 2.0 'afnetworking ', '<= 2.0' // use version 7 pod 'afnetworking', '~> 0.1.2 '// use version 8 pods 'afnetworking' with a value greater than or equal to 0.1.2 but less than 0.2,' ~> 0.1 '// use a version 9 pod 'afnetworking',' ~> 0' // use the latest version, which is the same as the specified dependent library version.
Here is more about CocoaPods and Podfile. lock.
13. If the pod install remains in the Updating local specs repositories, try pod install -- verbose -- no-repo-update.