Installation and use of third-party network request library Alamofire in Swift
Alamofire is one of the more popular network request libraries in Swift: Https://github.com/Alamofire/Alamofire. Here we introduce the installation and use of this library.
One, install package management tool Cocoapods
As the domestic easy to be wall, the speed is very slow, here we use the provided source http://ruby.taobao.org/
Gem sources--remove Https://rubygems.org/gem sources–a Http://ruby.taobao.org/gem sources–l
First use the following command to upgrade the Ruby version:
update --system
Wait for the update to complete.
Then use the Gem command to download the installation Cocoapods:
install cocoapods
After installation, call the following command to make the Cocoapods settings (you can see if the installation was successful through the pod command):
pod setup
After executing the above command, the installation is successful! The following describes how to use Cocoapods.
Second, use cocoapods download Alamofire Library
We first create a swift demo project named demo, and a project file is generated demo.xcodeproj
Let's see if we can support the Alamofire three-party library
Pod Search Alamofire
Normally, some information about the three-party library is output, as well as the version number.
The following important work has begun.
Create a Pod profile with the file name Podfile, note that there is no extension, for an introduction to the Podfile file refer to: http://blog.haohtml.com/archives/16970.
The contents are as follows:
SOURCE ' Https://github.com/CocoaPods/Specs.git ' Platform:ios, ' 9.0 ' Use_frameworks!target ' demo ' dopod ' Alamofire ',: git = ' https://github.com/Alamofire/Alamofire.git ',: Tag = ' 3.4.0 ' End
Then execute the Install command
Pod Install
After the successful installation, there will be more than one demo.xcworkspace of the project files, later we develop again, we need to use this file, discard the original demo.xcodeproj file.
At this point the project directory is as follows:
The Pods folder contains the downloaded third-party dependent libraries.
Open the project file Demo.xcworkspace. The project structure at this point is as follows:
Third, the use of Alamofire library
We opened the Viewcontroller.swift and introduced the Alamofir library. Add an "import Alamofire" below the import UIKit line in the header of the file , make sure you do this step, or you will not use the library's API.
Then the super in the viewdidload () method . Viewdidload() The following line, put Https://github.com/Alamofire/Alamofire one of the API code in, and then run the project, You can see some relevant information in the output panel of Xcode, which shows that these APIs can be called normally.
If we need to install another library, such as the layout of the three-party library Snapkit, first search
Pod Search Snapkit
Results such as:
Add a line "pod ' snapkit ', ' ~> 0.14.0 '" in the Target xxx do ... end block as prompted, and the file reads as follows: Podfile
SOURCE ' Https://github.com/CocoaPods/Specs.git ' Platform:ios, ' 9.0 ' Use_frameworks!target ' demo ' dopod ' Alamofire ',: git = ' https://github.com/Alamofire/Alamofire.git ',: Tag = ' 3.4.0 ' pod ' snapkit ', ' ~> 0.14.0 ' end
Execute the installation command again
Pod Install
Can. You should see the Snapkit library in the Pods directory at this time. finally
Related articles
- @available and #available的用法 in Swift
- View layout in swift third-party library Snapkit (Swift version of Masonry)
- Use of iOS9 in Uialertcontroller
- Action Segue in iOS8
- Understanding of delegated delegate in Swift
Installation and use of third-party network request library Alamofire in Swift