|
|
@@ -133,11 +133,11 @@ Alamofire.request(.GET, "http://httpbin.org/get")
|
|
|
|
|
|
```swift
|
|
|
Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
|
|
|
- .response { (request, response, data, error) in
|
|
|
- println(request)
|
|
|
- println(response)
|
|
|
- println(error)
|
|
|
- }
|
|
|
+ .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](https://developer.apple.com/library/ios/qa/qa1693/_index.html) for doing it this way.
|
|
|
@@ -157,8 +157,8 @@ Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
|
|
|
|
|
|
```swift
|
|
|
Alamofire.request(.GET, "http://httpbin.org/get")
|
|
|
- .responseString { (_, _, string, _) in
|
|
|
- println(string)
|
|
|
+ .responseString { _, _, string, _ in
|
|
|
+ println(string)
|
|
|
}
|
|
|
```
|
|
|
|
|
|
@@ -166,8 +166,8 @@ Alamofire.request(.GET, "http://httpbin.org/get")
|
|
|
|
|
|
```swift
|
|
|
Alamofire.request(.GET, "http://httpbin.org/get")
|
|
|
- .responseJSON { (_, _, JSON, _) in
|
|
|
- println(JSON)
|
|
|
+ .responseJSON { _, _, JSON, _ in
|
|
|
+ println(JSON)
|
|
|
}
|
|
|
```
|
|
|
|
|
|
@@ -177,11 +177,11 @@ Response handlers can even be chained:
|
|
|
|
|
|
```swift
|
|
|
Alamofire.request(.GET, "http://httpbin.org/get")
|
|
|
- .responseString { (_, _, string, _) in
|
|
|
- println(string)
|
|
|
+ .responseString { _, _, string, _ in
|
|
|
+ println(string)
|
|
|
}
|
|
|
- .responseJSON { (_, _, JSON, _) in
|
|
|
- println(JSON)
|
|
|
+ .responseJSON { _, _, JSON, _ in
|
|
|
+ println(JSON)
|
|
|
}
|
|
|
```
|
|
|
|
|
|
@@ -247,12 +247,10 @@ Parameters can also be encoded as JSON, Property List, or any custom format, usi
|
|
|
enum ParameterEncoding {
|
|
|
case URL
|
|
|
case JSON
|
|
|
- case PropertyList(format: NSPropertyListFormat,
|
|
|
- options: NSPropertyListWriteOptions)
|
|
|
+ case PropertyList(format: NSPropertyListFormat, options: NSPropertyListWriteOptions)
|
|
|
+ case Custom((URLRequestConvertible, [String: AnyObject]?) -> (NSMutableURLRequest, NSError?))
|
|
|
|
|
|
- func encode(request: NSURLRequest,
|
|
|
- parameters: [String: AnyObject]?) ->
|
|
|
- (NSURLRequest, NSError?)
|
|
|
+ func encode(request: NSURLRequest, parameters: [String: AnyObject]?) -> (NSURLRequest, NSError?)
|
|
|
{ ... }
|
|
|
}
|
|
|
```
|
|
|
@@ -303,10 +301,7 @@ Caching is handled on the system framework level by [`NSURLCache`](https://devel
|
|
|
#### Uploading a File
|
|
|
|
|
|
```swift
|
|
|
-let fileURL = NSBundle.mainBundle()
|
|
|
- .URLForResource("Default",
|
|
|
- withExtension: "png")
|
|
|
-
|
|
|
+let fileURL = NSBundle.mainBundle().URLForResource("Default", withExtension: "png")
|
|
|
Alamofire.upload(.POST, "http://httpbin.org/post", file: fileURL)
|
|
|
```
|
|
|
|
|
|
@@ -314,10 +309,10 @@ Alamofire.upload(.POST, "http://httpbin.org/post", file: fileURL)
|
|
|
|
|
|
```swift
|
|
|
Alamofire.upload(.POST, "http://httpbin.org/post", file: fileURL)
|
|
|
- .progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
|
|
|
+ .progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
|
|
|
println(totalBytesWritten)
|
|
|
}
|
|
|
- .responseJSON { (request, response, JSON, error) in
|
|
|
+ .responseJSON { request, response, JSON, error in
|
|
|
println(JSON)
|
|
|
}
|
|
|
```
|
|
|
@@ -335,11 +330,11 @@ Alamofire.upload(
|
|
|
encodingCompletion: { encodingResult in
|
|
|
switch encodingResult {
|
|
|
case .Success(let upload, _, _):
|
|
|
- upload.responseJSON { request, response, JSON, error in
|
|
|
- println(JSON)
|
|
|
- }
|
|
|
+ upload.responseJSON { request, response, JSON, error in
|
|
|
+ println(JSON)
|
|
|
+ }
|
|
|
case .Failure(let encodingError):
|
|
|
- println(encodingError)
|
|
|
+ println(encodingError)
|
|
|
}
|
|
|
}
|
|
|
)
|
|
|
@@ -355,13 +350,10 @@ Alamofire.upload(
|
|
|
#### Downloading a File
|
|
|
|
|
|
```swift
|
|
|
-Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: { (temporaryURL, response) in
|
|
|
- if let directoryURL = NSFileManager.defaultManager()
|
|
|
- .URLsForDirectory(.DocumentDirectory,
|
|
|
- inDomains: .UserDomainMask)[0]
|
|
|
- as? NSURL {
|
|
|
+Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: { temporaryURL, response in
|
|
|
+ let fileManager = NSFileManager.defaultManager()
|
|
|
+ if let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as? NSURL {
|
|
|
let pathComponent = response.suggestedFilename
|
|
|
-
|
|
|
return directoryURL.URLByAppendingPathComponent(pathComponent!)
|
|
|
}
|
|
|
|
|
|
@@ -373,7 +365,6 @@ Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: { (tempor
|
|
|
|
|
|
```swift
|
|
|
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
|
|
|
-
|
|
|
Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: destination)
|
|
|
```
|
|
|
|
|
|
@@ -381,10 +372,10 @@ Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: destinati
|
|
|
|
|
|
```swift
|
|
|
Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: destination)
|
|
|
- .progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
|
|
|
+ .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
|
|
|
println(totalBytesRead)
|
|
|
}
|
|
|
- .response { (request, response, _, error) in
|
|
|
+ .response { request, response, _, error in
|
|
|
println(response)
|
|
|
}
|
|
|
```
|
|
|
@@ -408,7 +399,7 @@ let password = "password"
|
|
|
|
|
|
Alamofire.request(.GET, "https://httpbin.org/basic-auth/\(user)/\(password)")
|
|
|
.authenticate(user: user, password: password)
|
|
|
- .response {(request, response, _, error) in
|
|
|
+ .response { request, response, _, error in
|
|
|
println(response)
|
|
|
}
|
|
|
```
|
|
|
@@ -423,7 +414,7 @@ let credential = NSURLCredential(user: user, password: password, persistence: .F
|
|
|
|
|
|
Alamofire.request(.GET, "https://httpbin.org/basic-auth/\(user)/\(password)")
|
|
|
.authenticate(usingCredential: credential)
|
|
|
- .response {(request, response, _, error) in
|
|
|
+ .response { request, response, _, error in
|
|
|
println(response)
|
|
|
}
|
|
|
```
|
|
|
@@ -438,7 +429,7 @@ By default, Alamofire treats any completed request to be successful, regardless
|
|
|
Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
|
|
|
.validate(statusCode: 200..<300)
|
|
|
.validate(contentType: ["application/json"])
|
|
|
- .response { (_, _, _, error) in
|
|
|
+ .response { _, _, _, error in
|
|
|
println(error)
|
|
|
}
|
|
|
```
|
|
|
@@ -450,8 +441,8 @@ Automatically validates status code within `200...299` range, and that the `Cont
|
|
|
```swift
|
|
|
Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
|
|
|
.validate()
|
|
|
- .response { (_, _, _, error) in
|
|
|
- println(error)
|
|
|
+ .response { _, _, _, error in
|
|
|
+ println(error)
|
|
|
}
|
|
|
```
|
|
|
|
|
|
@@ -571,7 +562,7 @@ For example, here's how a response handler using [Ono](https://github.com/mattt/
|
|
|
```swift
|
|
|
extension Request {
|
|
|
class func XMLResponseSerializer() -> Serializer {
|
|
|
- return { (request, response, data) in
|
|
|
+ return { request, response, data in
|
|
|
if data == nil {
|
|
|
return (nil, nil)
|
|
|
}
|
|
|
@@ -584,9 +575,9 @@ extension Request {
|
|
|
}
|
|
|
|
|
|
func responseXMLDocument(completionHandler: (NSURLRequest, NSHTTPURLResponse?, ONOXMLDocument?, NSError?) -> Void) -> Self {
|
|
|
- return response(serializer: Request.XMLResponseSerializer(), completionHandler: { (request, response, XML, error) in
|
|
|
+ return response(serializer: Request.XMLResponseSerializer()) { request, response, XML, error in
|
|
|
completionHandler(request, response, XML as? ONOXMLDocument, error)
|
|
|
- })
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
@@ -613,9 +604,9 @@ extension Alamofire.Request {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return response(serializer: serializer, completionHandler: { (request, response, object, error) in
|
|
|
+ return response(serializer: serializer) { request, response, object, error in
|
|
|
completionHandler(request, response, object as? T, error)
|
|
|
- })
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
@@ -634,7 +625,7 @@ final class User: ResponseObjectSerializable {
|
|
|
|
|
|
```swift
|
|
|
Alamofire.request(.GET, "http://example.com/users/mattt")
|
|
|
- .responseObject { (_, _, user: User?, _) in
|
|
|
+ .responseObject { _, _, user: User?, _ in
|
|
|
println(user)
|
|
|
}
|
|
|
```
|
|
|
@@ -648,7 +639,7 @@ The same approach can also be used to handle endpoints that return a representat
|
|
|
|
|
|
extension Alamofire.Request {
|
|
|
public func responseCollection<T: ResponseCollectionSerializable>(completionHandler: (NSURLRequest, NSHTTPURLResponse?, [T]?, NSError?) -> Void) -> Self {
|
|
|
- let serializer: Serializer = { (request, response, data) in
|
|
|
+ let serializer: Serializer = { request, response, data in
|
|
|
let JSONSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
|
|
|
let (JSON: AnyObject?, serializationError) = JSONSerializer(request, response, data)
|
|
|
|
|
|
@@ -659,9 +650,9 @@ extension Alamofire.Request {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return response(serializer: serializer, completionHandler: { (request, response, object, error) in
|
|
|
+ return response(serializer: serializer) { request, response, object, error in
|
|
|
completionHandler(request, response, object as? [T], error)
|
|
|
- })
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
@@ -694,7 +685,7 @@ extension Alamofire.Request {
|
|
|
|
|
|
```swift
|
|
|
Alamofire.request(.GET, "http://example.com/users")
|
|
|
- .responseCollection { (_, _, users: [User]?, _) in
|
|
|
+ .responseCollection { _, _, users: [User]?, _ in
|
|
|
println(users)
|
|
|
}
|
|
|
```
|