使用簡單、功能驚喜,基于 NSURLSession 的網(wǎng)絡(luò)封裝庫。功能包括帶身份驗證請求,支持單元測試(mocking/stubbing),異步執(zhí)行,圖片下載及緩存等實用特性。
GET示例
let networking = Networking(baseURL: "http://httpbin.org")
networking.get("/get") { result in
switch result {
case .success(let response):
let json = response.dictionaryBody
// Do something with JSON, you can also get arrayBody
case .failure(let response):
// Handle error
}
}
POST示例
let networking = Networking(baseURL: "http://httpbin.org")
networking.post("/post", parameters: ["username" : "jameson", "password" : "secret"]) { result in
/*
{
"json" : {
"username" : "jameson",
"password" : "secret"
},
"url" : "http://httpbin.org/post",
"data" : "{"password" : "secret","username" : "jameson"}",
"headers" : {
"Accept" : "application/json",
"Content-Type" : "application/json",
"Host" : "httpbin.org",
"Content-Length" : "44",
"Accept-Language" : "en-us"
}
}
*/
}