Melon是一个Swift HTTP网络库。
当前版本仅支持CocoaPods。
pod 'Melon', '~> 1.0.1'
pod update
GET
Melon.GET("https://httpbin.org/get")
.addHeaders(["a":"a", "b":"b"])
.addParams(["name": "caiyanzhi", "age":26])
.onNetworkError({ error in
}).responseJSON { (jsonObject, response) in
print("jsonObject : \(jsonObject)" )
}
POST
Melon.POST("https://httpbin.org/post")
.addHeaders(["a":"a", "b":"b"])
.addParams(["name": "caiyanzhi", "age":26])
.onNetworkError({ error in
}).responseJSON { (jsonObject, response) in
print("jsonObject : \(jsonObject)")
}
上传
let melon =
Melon.POST("https://httpbin.org/post")
.addHeaders(["a":"a", "b":"b"])
.addParams(["name": "caiyanzhi", "age":26])
.addFiles([formdata])
.uploadProgress({ (bytesSent, totalBytesSent, totalBytesExpectedToSend) in
Melon.Print("totalBytesSent:\(totalBytesSent)")
Melon.Print("totalBytesExpectedToSend:\(totalBytesExpectedToSend)")
})
.onNetworkError({ error in
}).responseJSON { (jsonObject, response) in
print("jsonObject : \(jsonObject)")
}
下载
let melon =
Melon.Download("https://www.google.co.jp/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png").onNetworkError ({ error in
}).downloadProgress({ (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
print("bytesWritten:\(bytesWritten)")
print("totalBytesWritten:\(totalBytesWritten)")
print("totalBytesExpectedToWrite:\(totalBytesExpectedToWrite)")
print("progress:\(Double(totalBytesWritten) / Double(totalBytesExpectedToWrite))")
}).responseData { (data, reponse) in
if let data = data {
let image = UIImage(data: data)
print("image: \(image)")
}
}
取消
you can use cancel method to cancel request
melon.cancel()
melon.cancel {
// cancel callback
}
Clone all files in the Melon directory into your project.