CocoaPods, a package management tool developed by IOS, is almost the industry standard for OBJECTIVE-C. It provides us with a very convenient package management function. And Apple has officially released the swift language for more than half a year, Swift as the IOS development language released, quickly by developers to accept. What followed was a growing use of Swift to write a third-party library management problem. The latest preview version of CocoaPods already supports the Swift language. This is a great boon for friends who have started to develop with Swift.
CocoaPods Introduction
This section is an introductory introduction to CocoaPods, and if you have already understood or used CocoaPods
it, you can go directly to this section and continue with the following. Almost every relatively senior OBJECTIVE-C developer will be familiar with the CocoaPods
comparison. CocoaPods
is a package management platform for the development Library under IOS and MAC OS platforms. More generally, it is a set of tools that let us not export to find third-party libraries, but to help us manage the newer versions of these libraries.
Let's use a simple example to illustrate CocoaPods
:
If we are now using OBJECTIVE-C to develop a project, we use a lot of third-party libraries, we will use afnetworking to handle network operations, but also use Egotableviewpullrefresh to build a pull-up refresh TableView
interface, but also to use the Svprogresshud as our loading progress tip. Use FMDB for Sqlite
database operations.
So what are we going to do? Let's go here https://github.com/AFNetworking/AFNetworking download AFNetworking
The code package, unzip it, copy it to our project's root directory, Then refer it to our project. Next, EGOTableViewPullRefresh
the SVProgressHUD
FMDB
same operation, download , unzip , copy , reference , and so on. If other system libraries are also referenced in these libraries, we will also configure the references to these libraries in the project settings, otherwise it will cause compilation errors. such as FMDB
referencing the sqlite3.lib
library, so we have to include this system library into the project.
I feel a bit of trouble oh.
In general, more or less of the project will use this 5, 6 third-party libraries, plus we have to do some data statistics, but also to add their third-party library. If some of these third-party libraries have newer versions, and we need this update, first open the download page for that library, then download , copy , and delete the operations.
Is there a sense of disorder? Just like him.
Our thinking is disrupted by these chores such as copying, pasting, and file manipulation. And these libraries are still out of order, like one day we want to know which third-party libraries are referenced in the project, and we'll find them in the code.
And CocoaPods
the emergence is to solve this problem. Let's take a look at CocoaPods
how use handles the above problem.
If we want to use CocoaPods
references to these libraries, first we need to create a file in the root directory of the project Podfile
:
platform :ios, ‘8.0‘source ‘https://github.com/CocoaPods/Specs.git‘pod ‘AFNetworking‘pod ‘EGOTableViewPullRefresh‘pod ‘SVProgressHUD‘pod ‘FMDB‘
The command is then also run in the root directory pod install
. CocoaPods
will download and automate the creation of a variety of referential dependencies for us. Once the command has been run, all of our third-party libraries are referenced. is not much more convenient, just need to run a command to complete all these tedious operations.
If we need to update these libraries, just run this command.pod update
Does it feel like it's suddenly enlightened?
With CocoaPods
our management of a variety of third-party libraries for a lot of convenience, just need to write down in the Podfile to use the library, and then run the command to complete. If we want to determine if the library we need is CocoaPods
available in, we can search for it on the official website, http://cocoapods.org
CocoaPods Support for Swift
The latest preview version of CocoaPods has provided support for the Swift project. Let's describe the installation and use steps below.
Because CocoaPods is running on the ruby
environment, we first need to install its operating environment.
Install ruby
, recommend everyone use rvm
to install, enter its official website Http://rvm.io we will see RVM's homepage introduction. We install it through a single command.rvm
\curl -sSL https://get.rvm.io | bash -s stable
Once installed rvm
, we'll use it to install the ruby
environment:
rvm install 2.1.1
This command represents that we are going to install the 2.1.1 version of Ruby, and then RVM will automatically do the rest for us.
Once our Ruby is installed, we can use it ruby --version
to determine if the installation was successful, and if the installation succeeds, we will receive such a prompt:
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin12.0]
Once the ruby installation is successful, we can install it CocoaPods
and continue to enter this command:
sudo gem install cocoapods --pre
After executing this command, we are ready to use it CocoaPods
. We note the --pre
command-line arguments, which are used to install the preview version, which means that CocoaPods
we can only use it in the Swift project if it is installed with this parameter.
Now we can use it, we create a Swift project in Xcode. We then create the Podfile file in the root directory of this project.
The root contents of our project are as follows:
We use the command line to enter this directory and enter a touch Podfile
command to create the file.
$ touch Podfile
Once created, our catalog is like this:
We can then CocoaPods
search the home page for the libraries we want to use to determine the exact names of these libraries. Like we're searching the Alamofire library.
We see that in the search results, the exact name of the library and the current version number are displayed. Next, we can edit the file we just created Podfile
.
platform :ios, ‘8.0‘source ‘https://github.com/CocoaPods/Specs.git‘pod ‘Alamofire‘
After editing, we run the command in the root directory of the project pod install
and we see the output as follows:
Analyzing dependenciesDownloading dependenciesInstalling Alamofire (1.1.4)Generating Pods projectIntegrating client project[!] From now on use `cocoapodsSample.xcworkspace`.
We see pod
commands that do these things for us, Analyzing dependencies
is to analyze the libraries in our project and the Podfile
libraries listed in, which need to be downloaded, what needs to be updated, and so on.
Once the analysis is done, it will be done Downloading dependencies
to download the libraries that need to be installed. Of course, the process is completely automated.
The downloaded library will then be installed Installing Alamofire (1.1.4)
.
CocosPods
These third-party libraries are then generated with a separate Xcode project.
Finally, it creates a Xcworkspace file that associates our project with a third-party library project.
Note the last line of output:
[!] From now on use `cocoapodsSample.xcworkspace`.
In other words, CocoaPods
after use, we will use workspace to open the project. Then we'll just press CocoaPods
the prompt to open workspace
:
$ open cocoapodsSample.xcworkspace
We saw two items in workspace, one for our app project and the other for the third-party library integration project created by CocoaPods:
Let's try using the library we just integrated in our project Alamofire
:
Compile, and run. Successfully, and our app correctly outputs the content in the console.
When we integrated this library, did not do anything like, download, copy these operations. Only one command was run, and then we were able to use the Alamofire library properly in the project.
For more articles, visit: www.theswiftworld.com
More good text, sweep code attention to the public number:
Swift Tips-When Swift meets CocoaPods