Alamofire 是 Swift 语言的 HTTP 网络开发工具包,功能强大,支持各种 HTTP Method、JSON、文件上传、文件下载和多种认证方法。
AFNetworking的作者全新力作,原生Swfit语言编写,基本可以满足Swift中网络编程开发。
github的URL为:
https://github.com/Alamofire/Alamofire
git clone的URL为
https://github.com/Alamofire/Alamofire.git
直接转一下官网的用法介绍
Alamofire is an HTTP networking library written in Swift, from the creator of AFNetworking.
Features
- Chainable Request / Response methods
- URL / JSON / plist Parameter Encoding
- Upload File / Data / Stream
- Download using Request or Resume data
- Authentication with NSURLCredential
- HTTP Response Validation
- Progress Closure & NSProgress
- cURL Debug Output
- Comprehensive Unit Test Coverage
- Complete Documentation
Requirements
- iOS 7.0+ / Mac OS X 10.9+
- Xcode 6.1
Communication
- If you need help, use Stack Overflow. (Tag 'alamofire')
- If you'd like to ask a general question, use Stack Overflow.
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request.
Installation
For application targets that do not support embedded frameworks, such as iOS 7, Alamofire can be integrated by including the
Alamofire.swift
source file directly, wrapping the top-level types instruct Alamofire
to simulate a namespace. Yes, this sucks.
Due to the current lack of proper infrastructure for Swift dependency management, using Alamofire in your project requires the following steps:
- Add Alamofire as a submodule by opening the Terminal,
cd
-ing into your top-level project directory, and entering the commandgit submodule add https://github.com/Alamofire/Alamofire.git
- Open the
Alamofire
folder, and dragAlamofire.xcodeproj
into the file navigator of your app project. - In Xcode, navigate to the target configuration window by clicking on the blue project icon, and selecting the application target under the "Targets" heading in the sidebar.
- Ensure that the deployment target of Alamofire.framework matches that of the application target.
- In the tab bar at the top of that window, open the "Build Phases" panel.
- Expand the "Target Dependencies" group, and add
Alamofire.framework
. - Click on the
+
button at the top left of the panel and select "New Copy Files Phase". Rename this new phase to "Copy Frameworks", set the "Destination" to "Frameworks", and addAlamofire.framework
.
Usage
Making a Request
import Alamofire
Alamofire.request(.GET, "http://httpbin.org/get")
Response Handling
Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
.response { (request, response, data, error) in
println(request)
println(response)
println(error)
}
Networking in Alamofire is done asynchronously. Asynchronous programming may be a source of frustration to programmers unfamiliar with the concept, but there are very good reasons for doing it this way.
Rather than blocking execution to wait for a response from the server, a callback is specified to handle the response once it's received. The result of a request is only available inside the scope of a response handler. Any execution contingent on the response or data received from the server must be done within a handler.
Response Serialization
Built-in Response Methods
response()
responseString(encoding: NSStringEncoding)
responseJSON(options: NSJSONReadingOptions)
responsePropertyList(options: NSPropertyListReadOptions)
Response String Handler
Alamofire.request(.GET, "http://httpbin.org/get")
.responseString { (_, _, string, _) in
println(string)
}
Response JSON Handler
Alamofire.request(.GET, "http://httpbin.org/get")
.responseJSON { (_, _, JSON, _) in
println(JSON)
}
Chained Response Handlers
Response handlers can even be chained:
Alamofire.request(.GET, "http://httpbin.org/get")
.responseString { (_, _, string, _) in
println(string)
}
.responseJSON { (_, _, JSON, _) in
println(JSON)
}
HTTP Methods
Alamofire.Method
lists the HTTP methods defined in RFC 7231 §4.3