Swift-cocoapods installation and use of detailed (automatic installation management of the third Third-party)

Source: Internet
Author: User
Tags versions uikit install cocoapods

When we develop, we often need to introduce some third-party libraries (e.g. Alamofire, Swiftyjson, etc.). The past practice was to download the libraries and introduce them into the project. If you have dependencies on other libraries, you will also need to manually add these dependent libraries. The point is that if there are any updates behind these third-party libraries, we'll have to delete the original libraries in the project. Repeat the previous steps. That would be a lot of trouble.

And after using Cocoapods, we don't need to do any of this work. Just do the configuration work, install the update of these third-party libraries, Cocoapods will automatically help us do a good job.

First, Cocoapods Introduction

(1) cocoapods is a Third-party Library management tool in iOS development. The goal is to enable us to automate, centralize, and visualize the management of third party open source libraries.
(2) Cocoapods can automatically resolve the dependencies between library and library, download the source code of the library. and create a Xcode workspace to connect these third-party libraries to our projects for our development and use.


Second, install Cocoapods

1, upgrade the gem to the latest version

sudo gem update--system

2, run the following command to install Cocoapods

sudo gem install-n/usr/local/bin cocoapods

Then execute the following command, if there is no error, it means that all installed successfully. (This execution will wait for some time, Cocoapods is downloading its information to the ~/.cocoapods.) There are probably more than 100 trillion. )
1
Pod Setup

3, after installation, perform the following command to view the version

Pod--version
Original: Swift-cocoapods installation and use of detailed (automatic installation management of the third Third-party)

4, to update the upgrade Cocoapods later, execute the following command

sudo gem update cocoapods


Third, the Cocoapods installation failure reason and the solution

1, after performing install has not responded, cannot install
Sometimes you will find that the execution of the install command has been unresponsive. This is because Ruby's default source uses rubygems.org. Due to domestic network reasons, access to the above resource file will fail intermittently.
(1) We can execute the following command to change the source to use the rubygems.org image provided by Taobao
1
Gem sources--add https://ruby.taobao.org/--remove https://rubygems.org/

(2) then execute the following command to ensure that only ruby.taobao.org

Gem Sources-l

(3) The final use of install will be able to successfully install Cocoapods.

2,ruby version too low

Installation failures can also occur if the version of native Ruby is too low.


(1) Install RVM (RVM allows us to have multiple versions of Ruby and can switch freely between multiple versions.) )
1
2
Curl-l Get.rvm.io | Bash-s Stable
SOURCE ~/.RVM/SCRIPTS/RVM

(2) After the completion of the following command, if the version number appears RVM installation success.

Rvm-v

(3) Execute RVM list known command to list all version information that can be installed by Ruby.

(4) We install a ruby version

RVM Install 2.2.4

(5) and set it to the default version. At this point we should be able to properly install the cocoapods.
1
RVM Use 2.2.4--default

(6) Use the RVM List command to view installed Ruby


(7) If you want to uninstall an installed Ruby version, execute the following command.

RVM Remove 2.1.4

Iv. Use of Cocoapods

Below we create a project (assuming the project name is hangge_1358), demonstrating that if you use Cocoapods to automatically download, import, and configure a Third-party library.
1, create Podfile
First go to the root of the project and create a blank podfile file.

cd/users/hangge/documents/code/hangge_1358
Touch Podfile

Original: Swift-cocoapods installation and use of detailed (automatic installation management of the third Third-party)

2, edit Podfile

We write in the Podfile file a third-party library that needs to be introduced, taking the Alamofire, Swiftyjson, and the two libraries as examples.

Platform:ios, ' 8.0 '
use_frameworks!

Target ' hangge_1358 ' do
Pod ' Alamofire '
Pod ' Swiftyjson '
End

3, start the import library

(1) Execute the following command to begin importing the Third-party libraries previously configured.

cd/users/hangge/documents/code/hangge_1358
Pod Install


(2) after successful execution. See the project root directory more than three new files: Hangge_1358.xcworkspace, podfile.lock files, Pods directory.
Original: Swift-cocoapods installation and use of detailed (automatic installation management of the third Third-party)

4, open the newly generated hangge_1358.xcworkspace
(1) Later we will need to use this newly generated hangge_1358.xcworkspace file to develop. Because the original project (hangge_1358.xcodeproj) settings have been changed, if we directly open the original project files to compile will be an error.
(2) After opening can see inside in addition to our project engineering, there is also a Pods project. The whole structure is clear.
Original: Swift-cocoapods installation and use of detailed (automatic installation management of the third Third-party)

5, testing the code
When developing, we just need to import the necessary libraries when we use them.

Import Uikit
Import Alamofire

Class Viewcontroller:uiviewcontroller {

Override Func Viewdidload () {
Super.viewdidload ()

Alamofire.request (. Get, "")
. responsestring {Response in
Print ("Success: \ (response.result.isSuccess)")
Print ("Response String: \ (response.result.value)")
}
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

6, add or remove third party libraries

Later, if you need to import the new library in the project, or remove the original library. Edit the Podfile file first, and then execute the install command.

Pod Install

7, update the third party library

If the referenced libraries do not change, but want to update these libraries to the latest version, execute the update command.

Pod Update


V. Import the third party library written by Objective-c

The example above is the creation of the SWIFT project and the introduction of the third party library written in Swift language. The same is true if you want to introduce Third-party libraries written by OC in Swift Engineering. And we do not need to create the bridge header file in our project as in the past, and refer the Objective-c header file.

Here is a picture cache library written by OC: Sdwebimage as an example.

Platform:ios, ' 8.0 '
use_frameworks!

Target ' hangge_1358 ' do
Pod ' Alamofire '
Pod ' Swiftyjson '
Pod ' Sdwebimage '
End

2, execute the installation command

Pod Install

3, the code directly to the Sdwebimage library import can be used

Import Uikit
Import Sdwebimage

Class Viewcontroller:uiviewcontroller {

Override Func Viewdidload () {
Super.viewdidload ()

Let ImageView = Uiimageview ()
Imageview.frame=cgrectmake (10,10,130,27.5)
Let URL = Nsurl (string: "/blog/images/logo.png")
Imageview.sd_setimagewithurl (URL)
Self.view.addSubview (ImageView)
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

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.