iOS development ~cocoapods using detailed instructions

Source: Internet
Author: User
Tags documentation install cocoapods

I. Overview

During iOS development, many Third-party libraries are referenced in the project, and Cocoapods (https://github.com/CocoaPods/CocoaPods) can be used to facilitate the unified management of these third-party libraries (from one pit to another). )。


Second, the installation

As the online tutorials are basically similar, but the details are not perfect, so the opportunity will be added here:

Note: To use cocoapods, it is necessary to download and install it, and the download installation cocoapods need ruby environment


1, Ruby Environment to build

The current installation environment is the Mac mini 10.8.5. Mac OS itself has Ruby, but it still updates the insurance because I failed the first time I installed it without updating Ruby.

A view the current Ruby version: Open the terminal input ruby-v (it was installed, but the next work failed with this version, so update ruby)

Ritekimac-mini:podtest lucky$ ruby-v
Ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]

b Update Ruby

The terminal enters the following command (Point Ruby Mirror to Taobao, avoid being wall, you know)

Gem Sources--remove https://rubygems.org/
Gem Sources-a http://ruby.taobao.org/
Gem sources-l (used to check for success with alternate mirror location)

ritekimac-mini:~ lucky$ gem sources--remove https://rubygems.org/https://rubygems.org/removed from
sources
ritekimac-mini:~ lucky$ gem sources-a http://ruby.taobao.org/
http://ruby.taobao.org/added to sources
ritekimac-mini:~ lucky$ Gem sources-l
* * * Current SOURCES * * * *

Added: (2016-01-20 update)

Taobao has turned off the HTTP protocol scene service, changing to HTTPS protocol.

Taobao Ruby address: https://ruby.taobao.org/2, download installation cocoapods

Terminal input: sudo gem install cocoapods


ritekimac-mini:~ lucky$ sudo gem install cocoapods

changelog:

# # 0.32.1

##### Bug fixes

* Fixed the PODFI Le ' default_subspec ' attribute in nested subspecs.  
  [Fabio Pelosin] [Irrationalfab]
 \ [#2050] (https://github.com/CocoaPods/CocoaPods/issues/2050)


successfully installed cocoapods-0.32.1
Installing RI documentation for cocoapods-0.32.1
/system/library/frameworks/ruby.framework/versions/1.8/usr/ Lib/ruby/1.8/rdoc/rdoc.rb:280:warning:conflicting ChDir during another chdir block
/system/library/frameworks/ Ruby.framework/versions/1.8/usr/lib/ruby/1.8/rdoc/rdoc.rb:287:warning:conflicting ChDir during another chdir block< C10/>done installing documentation for cocoapods after seconds
1 gem installed


So the download is installed Cocoapods


3. Use Cocoapods

A create a new project with the name Podtest



b terminal, CD to the total list of items (note: The head record containing the Podtest folder, Podtest.xcodeproj, Podtesttest)


C Establish podfile (config file)

Next step, the terminal enters Vim Podfile



Keyboard input I, enter edit mode, enter

Platform:ios, ' 7.0 '
Pod ' Mbprogresshud ', ' ~> 0.8 '


Then press ESC and enter the ":" Number into VIM command mode, then enter WQ after the colon



Note: The keyboard input: You can enter Wq. Find Podtest One more Podfile file in the total list of items after carriage return



The exciting time has come: Determine the terminal CD to the project's total directory, then enter the pod install and wait for a moment, about 3 minutes.



To view the project root directory:



Note: Now open the item is not clicked Podtest.xodeproj, but click Podtest.xcworkspace

For the changes in the project, there are several points to explain:

A, third party library will be used as a static library to introduce engineering use

Cocoapods will have all the Third-party libraries in target form a project called pods, which will be placed under the newly generated pods directory. The entire third party library project will generate a static library called LIBPODS.A to be provided to engineering use.

B, Xcworkspace (Podtest.xcworkspace) is the role of management engineering and Third-party libraries

Podtest Engineering and Pods engineering are organized and managed in the form of workspace.

After you open the project, see the project structure and test:



Run Result:



Add:

1, the basic installation and use of cocoapods are described in detail, but there are some additions, when you need to import multiple third parties at the same time how to do.

This needs to be modified Podfile, the one that is saved in the project root directory with vim, the Podfile file is modified, and the pod Install command needs to be executed again.

For example:

Platform:ios

Pod ' Jsonkit ', ' ~> 1.4 '

Pod ' afnetworking ', ' ~> 2.0 '


2, Cocoapods can find you want the third party library

Terminal input command: Pod search UI

Crazy, how do I find such a popular keyword, a lot of library ~ ~


Then edit the Podfile file again, and follow the previous steps to import more libraries into the project.

3, above "Establish podfile (configuration file) Next step, terminal input vim podfile" step, if you are not accustomed to using the VIM editor, you can also use the Linux command touch, generate an empty podfile file, and then use the other text editor can, For example:

lizhongfudemacbook-pro:~ lizhongfu$ cd/users/lizhongfu/desktop/testproject 
Lizhongfudemacbook-pro: TestProject lizhongfu$ Touch Podfile
After editing the Podfile file, you can perform pod install third party import (see: Add 1th), Cocoapods began to do for us to download the source code, configure dependencies, the introduction of the required framework, and some other columns.

4, if the project has been generated cocoapods management, you can edit the Podfile in Xcode, edit save, do not forget the CD to the root directory, call the pod Install command

5. When a project has multiple target, you need to configure the Podfile file to support the newly added target, otherwise only target that is generated when the project is created by default is supported:

A, if you create a new target, name second, and second the same third-party support that is required for test two target, that is, using the same pods dependent library, you can use the

Link_with Keyword:

Link_with ' Test ', ' Second '
platform:ios
platform:ios, ' 9.0 '  
pod ' afnetworking ', ' ~> 2.0 '

b, if different target requires a different dependent library, you can

Platform:ios
target: ' Test ' do pod ' reachability ' pod '
sbjson '   
pod ' afnetworking '  
  
end Target: ' Second ' do
pod ' openudid ' 
end


6, appear/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)

Reopen a terminal, then pod install, not sudo pod install.

7, if the $ pod install encountered:

Analyzing dependencies
Fetching Podspec for ' Uaappreviewmanager '. `
[!] Unable to satisfy the following requirements:
-' Uaappreviewmanager (from '. ') ' required by ' podfile '

8, if you want to remove Cocoapods, you can:

A. Delete the Podfile, Podfile.lock, and Pods folders under the Engineering folder

B. Deleting xcworkspace files

C. Use xcodeproj file to open engineering, delete pods.xcconfig and LIBPODS.A references under frameworks group

D. Delete Check Pods manifest.lock and copy Pods resources under build phases in the project setup



PS: If the Cocoapods integrated into the project, accidentally modified or deleted its related files resulting in the inability to pass for example: accidentally

Pods.xcconfig to delete then appear diff:/. /podfile.lock:no such file or directory, after deleting the cocoapods with the above method,

Just $sudo pod install again.

If there is a permission problem when compiling, the Engineering folder $sudo chmod 777 path-to-project-folder/*

$sudo Chown 777 path-to-project-folder/*

9, when the library is normally imported, but still cannot find the related header file, you can:

Write Paths in the targets-> search Paths-> User Header Search ${srcroot and then change the following argument to recursive:



10, xcode-ld:library not found For-lpods

When a new target is added and the pod is install, the Create project is the target build created by default Phases references the old. A, such as LIBPODS.A, when the new Targget is added, LIBPODS.A has become libpods-test.a, and the new target name is Second, and the dependent. A is libpods-second.a. So LIBPODS.A is no longer referenced at this time and will not be generated, and if any reference is made to the error, the solution is to remove the unwanted. A reference in the target's build phases, such as LIBPODS.A

Related 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.