Use Cocoapods to do package dependency management for iOS programs

Source: Internet
Author: User
Tags install cocoapods

Objective

As each language develops to a single stage, a corresponding dependency management tool appears, or a central code repository. Like what

    • Java:maven,ivy
    • Ruby:gems
    • Python:pip, Easy_install
    • nodejs:npm

As iOS developers have grown, the industry has also come up with a dependency management tool for iOS programs called CocoaPods.

Cocoapods Introduction

Cocoapods is a tool for managing third-party open source code in iOS projects. The source code for the Cocoapods project is managed on GitHub. The project began on August 12, 2011, and after more than a year of development, it has now been submitted more than 1000 times and has continued to remain active and updated. Developing iOS projects inevitably uses third-party open source libraries, and the advent of cocoapods allows us to save time setting up and updating third-party open source libraries.

Take my previous development of the chalk Web iphone Client For example, which uses 14 third-party open source libraries. Before using cocoapods, I need to:

    1. Copy the relevant files from these third-party open source libraries to the project, or to Git's submodule, and these open source libraries often need to rely on some framework of the system, I need to manually add these frameworks one by one to the project dependencies, For example, the ASI network Library needs to add the following framework:cfnetwork, SystemConfiguration, Mobilecoreservices, Coregraphics and zlib.
    2. For regexkitlite This regular expression library, I also need to set the compilation parameters of-licucore
    3. Manually manage updates for these dependent packages.

These physical activities, though simple, have no technical content and waste time. After using Cocoapods, I only need to put the third-party open Source Library in a file named Podfile, and then execute the pod install. Cocoapods will automatically download the source code of these third-party open source libraries and set up the corresponding system dependencies and compilation parameters for my project.

Installation and use of Cocoapods installation

The installation is exceptionally simple, with Ruby on your Mac, and you can download the installation using Ruby's GEM command:

12
$ sudo gem install cocoapods$ pod setup

When the second row above executes, it will output Setting up CocoaPods master repo , but will wait for a longer time. This step is actually cocoapods to download its information into the ~/.cocoapods directory, if you wait too long, you can try to CD to that directory, use du -sh * to check the download progress.

If your gem is too old and may be problematic, try upgrading the gem with the following command:

1
sudo gem update --system

In addition, Ruby's software source rubygems.org because of the use of the Amazon Cloud service, so it was wall, need to update the source of Ruby:

123 
gem sources --remove https://rubygems.org/gem sources -a http://ruby.taobao.org/gem sources -l
Use

When using, you need to create a new file named Podfile, in the following format, the dependent library name is listed in the file.

123)45   
platform :iospod ‘JSONKit‘,       ‘~> 1.4‘pod ‘Reachability‘, ‘~> 3.0.0‘pod ‘ASIHTTPRequest‘pod ‘RegexKitLite‘

Then you put the edited Podfile file in your project root directory and execute the following command:

12
cd "your project home"pod install

Now that all of your third-party libraries have been downloaded and set up with compilation parameters and dependencies, you just have to remember the following 2 points:

    1. Use the. xcworkspace file generated by Cocoapods to open the project instead of the previous. xcodeproj file.
    2. Each time you change the Podfile file, you need to re-execute the pod install command.
Find a third-party library

If you don't know if there are any libraries you want in the Cocoapods managed library, then you can find them through the pod Search command, and here are all the available libraries I found with pod search JSON:

12345678910111213141516171819 20212223 242526 27282930 313233 34353637 383940 41424344     
$ pod Search JSONAnyjson(0.0.1)Encode/decode JSON by any means possible.-Homepage:https://github.com/mattt/anyjson-Source:https://github.com/mattt/anyjson.git-versions:0.0.1[Master Repo]Jsonkit(1.5pre)A Very High Performance objective-c JSON Library.-Homepage:https://github.com/johnezang/jsonkit-Source:git://github.com/johnezang/jsonkit.git-Versions:1.5pre, 1.4[Master Repo]Mtjsondictionary(0.0.4)An nsdictionary categoryFor when' re working with it converting To/from JSON. DEPRECATED, use Mtjsonutilsinstead.-Homepage:https://github.com/mysterioustrousers/mtjsondictionary.git-Source:https://github.com/mysterioustrousers/mtjsondictionary.git-versions:0.0.4, 0.0.3, 0.0.2[Master Repo]Mtjsonutils(0.1.0)An nsobject categoryFor working with JSON.-Homepage:https://github.com/mysterioustrousers/mtjsonutils.git-Source:https://github.com/mysterioustrousers/mtjsonutils.git-Versions:0.1.0, 0.0.1[Master Repo]Sbjson(3.1.1) This library implements strict JSON parsing and generation in objective-c. -Homepage:http://stig . github.com/json-framework/ -Source:https://github.com/stig/json-framework.git -versions:3.1.1, 3.1, 3.0.4 , 2.2.3 [master repo],Touchjson (1.0) Touchjson is an objective-c based parser and Generat Or for JSON encoded data. -Homepage:https://github.com/touchcode/touchjson -source:https://github.com /touchcode/touchjson.git -versions:1.0 [master repo]       
About. Gitignore

When you do pod install , besides Podfile, Cocoapods will generate a Podfile.lock file named, you should not add this file to the .gitignore . Because Podfile.lock the version of the current dependent library is locked, it will change if the version is not changed after multiple executions pod install pod update Podfile.lock . This can prevent the third-party library from upgrading to hang up the program when many people collaborate.

To generate a third-party library's help document

If you want Cococapods to help you build a third-party library's help documentation and integrate it into Xcode, install Appledoc with brew:

1
brew install appledoc

As for Appledoc, I had a special introduction to the "Document generation tool using Objective-c: Appledoc" In another blog earlier this year. The biggest advantage is that you can integrate the help document into Xcode so that when you tap the code, you can click the class name or the method name to display the appropriate help document.

Principle

Probably studied the principle of cocoapods, which is to put all the dependent libraries into another named Pods project, and then let the main project depends on the pods project, so that the source management work from the main project moved to the Pods project. Some of the technical details found are:

    1. The PODS project is eventually compiled into a file called LIBPODS.A, and the main project only needs to rely on this. a file.
    2. For resource files, Cocoapods provides a bash script called pods-resources.sh, which executes every time the project is compiled and copies the various resource files from the third-party library to the target directory.
    3. Cocoapods uses a file called Pods.xcconfig to set all dependencies and parameters at compile time.
Reprinted from: http://blog.devtang.com/blog/2012/12/02/use-cocoapod-to-manage-ios-lib-dependency/

Use Cocoapods to do package dependency management for iOS programs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.