Cowboy Tech

Alamofire简介

Making a Request

1
2
import Alamofire
Alamofire.request("https://httpbin.org/get")

Response JSON Handler

1
2
3
4
5
6
7
8
9
10
Alamofire.request("https://httpbin.org/get").responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization

if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}

Downloading Data to a File

Download data to a temporary file on-disk

1
2
3
4
5
Alamofire.download("https://httpbin.org/image/png").responseData { response in
if let data = response.result.value {
let image = UIImage(data: data)
}
}

Download File Destination

1
2
3
4
5
6
7
8
9
10
11
12
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendPathComponent("pig.png")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}

Alamofire.download(urlString, to: destination).response { response in
print(response)
if response.result.isSuccess, let imagePath = response.destinationURL?.path {
let image = UIImage(contentsOfFile: imagePath)
}
}

Suggested download destination

1
2
let destination = DownloadRequest.suggestedDownloadDestination(directory: .documentDirectory)
Alamofire.download("https://httpbin.org/image/png", to: destination)

Download Progress

1
2
3
4
5
6
7
8
9
Alamofire.download("https://httpbin.org/image/png")
.downloadProgress { progress in
print("Download Progress: \(progress.fractionCompleted)")
}
.responseData { response in
if let data = response.result.value {
let image = UIImage(data: data)
}
}

Download Progress with dispatchQueue

1
2
3
4
5
6
7
8
9
10
11
let utilityQueue = DispatchQueue.global(qos: .utility)

Alamofire.download("https://httpbin.org/image/png")
.downloadProgress(queue: utilityQueue) { progress in
print("Download Progress: \(progress.fractionCompleted)")
}
.responseData { response in
if let data = response.result.value {
let image = UIImage(data: data)
}
}

Resuming a Download

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class ImageRequestor {
private var resumeData: Data?
private var image: UIImage?

func fetchImage(completion: (UIImage?) -> Void) {
guard image == nil else { completion(image) ; return }

let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendPathComponent("pig.png")

return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}

let request: DownloadRequest

if let resumeData = resumeData {
request = Alamofire.download(resumingWith: resumeData)
} else {
request = Alamofire.download("https://httpbin.org/image/png")
}

request.responseData { response in
switch response.result {
case .success(let data):
self.image = UIImage(data: data)
case .failure:
self.resumeData = response.resumeData
}
}
}
}

Bug

Xcode的bug: 从cocoapods导入时,识别不了Alamofire。只要先Run一次,再import Alamofire就可以了