Cocoapods complete use of the article

Source: Internet
Author: User

Warm tip: The Xcode version used in this article is Xcode7.

First, what is Cocoapods?

Simply put, it is the management tool that provides the reliance on third-party libraries specifically for iOS engineering, and with Cocoapods, we can manage each third-party library separately, making it easier to manage the version of each third-party library, without requiring us to do too much configuration, Configured directly by the authors who provide support for the Cocoapods project, so that the third-party library of our project can be managed intuitively, centrally, and automatically.

Second, why do I need to use cocoapods?

We may have felt this way:

For each third-party library, framework, or SDK, we need to manually add the dependent libraries, configure the paths in the project buildsetting, and add the dependent system libraries in build phases. If you import third-party libraries that also rely on other third-party libraries, we also need to manually import and add engineering configurations separately.

When we need to update a third-party library, we have to manually remove the library, import a new library, and then configure it, which is rather cumbersome and meaningless work. When using Cocoapods management, we only need to change to a version, then perform pod update.

What do we do when we need to get rid of a third-party library? Remove the library, and then remove the configuration so that the work is clean. Isn't that a lot of trouble? When we use Cocoapods management, how do we do it? You only need to delete the statement that introduced the library in Podfile, and then perform the pod update.

When we started using Cocoapods to manage third-party libraries, we only needed quite a few configurations, and everything else was managed by cocoapods, and we used them more easily.

Third, how to install Cocoapods?

CocoaPods are built with Ruby and are installable with the default Ruby available on OS x. We recommend you to use the default Ruby.

Cocoapods is installed through Ruby, MAC OSX has a default Ruby version, it is recommended that we install Cocoapods through the default Ruby.

Use the following command to install:

In fact, this installation may not be successful, because the default Cocoapods URL is foreign and requires a VPN to access it, so we can change it one way:

1, first enter the gem sources--remove https://rubygems.org/

2, wait for a reflection, then enter the Gem sources-a http://ruby.taobao.org/

3. Verify successful replacement: Gem Sources-l

4, finally can be installed through the sudo gem install cocoapods normal cocoapods.

After the installation is complete, you can start using Cocoapods.

Iv. How to use Cocoapods?

To use Cocoapods, you need a podfile file. See, how we build podfile for all works, the following way is the basic way.



    1. Step one: Enter into the catalogue of the works we have created, here is Kvodemo

    2. Step Two: Create a new podfile with the Touch command

    3. The third step: Through VI podfile into the compilation Podfile

    4. Fourth step: Add a third-party library, for example, we added the afnetworking and Objectivesugar libraries, where we added the Afnetworking version is version 2.0, Objectivesugar version is 0.5.

    5. Press the ESC key, and then enter: Wq, you can save it. You can then install the third-party library by entering pod install in the terminal.

    6. After the installation is complete, we are no longer opening a project with the suffix. Xcodeproj, but instead open the workspace with the suffix. Xcworkspace.

For more advanced use of podfile, please refer to the official documentation: https://guides.cocoapods.org/using/the-podfile.html

1), the use in the OBJECTIVE-C project

In the project, we only need to introduce changes to the file can be used directly, for example, we introduced a third-party library masonry (pure Code automatic Layout), we can be in the OBJECTIVE-C project through the import header file.


Note that if you do this to find the header file, then we can try to introduce:

#import "Masonry.h" or through #import "Masonry/masonry.h"

If it still doesn't work, then you need to configure it in the project:

Search Paths in the project build settings, then add $ (srcroot) in user header search Paths and select recursive (that is, recursive lookup)

2). Use in Swift Engineering

I believe that the use of cocoapods in Swift Works has also encountered a lot of problems, especially how to import the module problem.

When I met this problem, I also searched for a lot of information on the Internet, but it was not the solution I wanted. There are two ways of doing this online:

    1. Through the Swift project can bridge Objectice-c Way, build a bridge-head.h (name casually), then go to build Settings, enter BRIDG in the search box, find objective-c bridging Header, options, assign the path to the head file to this option. :
      That is, the project name/bridge file name. H.

    2. In the bridged file that you just created, you can do so by #import "header file. h".

Although Objective-c is a third-party library, we can actually introduce it using Swift, that is, by importing the module name. So for the above way, I don't like it. Then look at another way online.

Original link: http://blog.shiqichan.com/How-To-Import-3rd-Lib-Into-Swift-Project/

This is managed by the Submodule way.

    1. Create a submodule and execute a command like this in the current project's sibling directory.

    2. Drag the resulting masonry.xcodeproj into the project, similar to the following:

    3. In the general of the Xcode project, click the + sign in embeded libraries and change our third-party library framework, similar to the following:

    4. Finally, you can use the Import module name directly in the project where you need to use it. Similar:

    5. If a multiplayer team is developed and we need to share it, then the other members will need to install it using the following command:
      This is to install or update submodule by hand. This is a very troublesome thing. Then according to the experience of the use, we download someone else's project down, executed the above command, installed the relevant modules, the operation of the project often error problems, that is, configuration problems. As a result, individuals do not like this way.

In fact, it is no longer necessary to do so in Xcode7, and is not validated for other Xcode versions.

Third-party libraries that we install through Cocoapods are automatically made into the framework, and then we just need to use the Import module name directly in the place where we use it. But sometimes a third-party library may appear directly through the Import module name, the hint is not found, that is, no smart prompt. At this point we can import it normally by clicking + on the Embeded libraries in the general of the Xcode project and then importing the framework. Additionally, if the imported third-party library is running, an error is made, similar to:

Dyld:library not loaded: @rpath/reactivecocoa.framework/reactivecocoa
Referenced from:/private/var/mobile/containers/bundle/application/31abc86a-c1bd-40dd-a117-d2c8f79a98fe/ Swiftgithubclient.app/swiftgithubclient Reason:image not found

Then we can solve this problem:

In Build Phases->link Binary with libraries-> the name of the library where the error was found, modify required to optional.

V. How to upgrade the Cocoapods version?

Upgrading the Cocoapods is very simple and requires just one command.

Under normal circumstances, only one command is required to upgrade:

However, it may be necessary to update the gem to upgrade cocoapods, so we may need to do this:
Reference: http://www.cnblogs.com/brycezhang/p/3675670.html

Vi. How to make your own open source project support Cocoapods?

1, the first step: Open the terminal and into the project directory. Here is my open source Hybmasonryautocellheight Open Source Library, which is used to automatically calculate the cell's height. 2. Step two: Create a podspec file. Created by command pod spec create.

3, the third step: Edit the Podspec file.

As you will see, this auto-generated is already a template, we need to do is to modify the relevant content, to supplement the content to meet our needs. For more advanced use, you will need to refer to the official documentation, and learn to use it step by step.

Look at the configuration of the Podspec file of the third-party library that I provided, take a look:

This step is critical and there are more things to configure. The main is s.source here must provide our git path, specify tag number and s.source_files here must specify the source code file of the library. Usually we put our library in the same class as the demo project. Xcodeproj, compare the Hybmasonryautocellheight directory here with the. Xcodeproj is the parent directory of the sibling, and then we can set it up more easily.
Then when we set up S.source_files, the first one is set to Hybmansonryautocellheight, and the second one is set to all. File of type {h,m}. For more complex settings, refer to the Podspec file in afnetworking.

What if our open Source Library relies on the system library?

# s.framework = ' someframework '//Remove #, set dependent system library name
# s.frameworks = ' someframework ', ' anotherframework '//Set multiple system library names

# s.library = ' iconv '//Set up a library that relies on only one system
# s.libraries = ' iconv ', ' xml2 '//Set up a library that relies on multiple systems

# s.xcconfig = {' header_search_paths ' + ' $ (sdkroot)/USR/INCLUDE/LIBXML2 '}//here is the engineering configuration so that the user does not need to handle it manually and is automatically processed by the pod.

What to do if the open source Library relies on other third-party libraries:

S.dependency ' Jsonkit ', ' ~> 1.4 '//Set our open Source library to rely on which third-party libraries and dependent version numbers.

4, the fourth step: here, we set the version number is 0.0.1, then the tag number is 0.0.1, so we also need to create a new tag, called 0.0.1, and then push to git:

Next, we need to verify that our configuration is correct:


Of course we can also directly use: Pod Lib lint to verify all.

If the validation passes, the prompt will appear:

Hybmasonryautocellheight (0.0.1)

Hybmasonryautocellheight passed validation.

If there is an error in our configuration just now, or if we need to modify some content and re-push, we can delete the remote tag 0.0.1 first, then repeat the fourth step above after the modification. The following is the Delete remote tag command:

6, Fifth step: After the verification passed, submitted to cocoapods. That is, by command pod Trunk push library name. Podspec to push to remote cocoapods:


If a warning appears, you need to modify the content to remove the warning. Once the operation is successful, we can search our library using the pod Search command.

For more detailed use, please refer to the official documentation: https://guides.cocoapods.org/making/making-a-cocoapod.html

Here is an article, written quite well: http://www.exiatian.com/cocoapods%E5%AE%89%E8%A3%85%E4%BD%BF%E7%94%A8%E5%8F%8A%E9%85%8D%E7%BD% ae%e7%a7%81%e6%9c%89%e5%ba%93/

Seven, how to generate cocoapods private library?

To make your library a private library, our code needs to be managed by Git.

This assumes that we have created a new project called Bookeffect, which is then push to the GIT remote server.

The first step: As in section Sixth, create a Podspec file and push the entire project to the GIT server side.

The second step: This is the way to introduce a private library. Pod ' demolib ',:git=> "Http://xxxxx.git" (replace with real git address)

For details, please refer to: http://www.cnblogs.com/superhappy/p/3468377.html

VIII. packaging a static library with Cocoapods

Here is not elaborate, but recommend an article: Http://www.cnblogs.com/brycezhang/p/4117180.html write very well.

Finally, thank you for reading this article carefully, thank you for your support. If you want to learn more, please follow the public number: Iosdevshares.

Reference: HTTPS://COCOAPODS.ORG/?Q=BLOCKSK

Cocoapods complete use of the article

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.