swift基于Alamofire的简易封装整理
2016-09-06 00:00
447 查看
摘要: Alamofire封装
//
// BLHttpSessionsRequest.swift
// ALSFinancial-Swift
//
// Created by 冰泪 on 16/8/23.
// Copyright © 2016年 冰泪. All rights reserved.
//网络请求类封装
import UIKit
import Alamofire
//创建请求类枚举
enum RequestType: Int {
case requestTypeGet
case requestTypePost
}
//创建一个闭包(注:oc中block)
typealias sendVlesClosure = (AnyObject?, NSError?)->Void
typealias uploadClosure = (AnyObject?, NSError?,Int64?,Int64?,Int64?)->Void
class BLHttpSessionsRequest: NSObject {
// --GET请求获取JSON数据
func BLGetJSONDataWithUrl(url: String, parameters: AnyObject, successed:(responseObject: AnyObject?) -> (), failed: (error: NSError?) -> ()) {
Alamofire.request(.GET, url, parameters: parameters as? [String : AnyObject]).responseJSON { (data: Response<AnyObject, NSError>) in
if data.result.isSuccess {
successed(responseObject: data.data)
}else {
failed(error: data.result.error)
}
}
}
// --POST请求获取JSON数据
func BLPostJSONDataWithUrl(url: String, parameters: AnyObject, successed:(responseObject: AnyObject?) -> (), failed: (error: NSError?) -> ()) {
//print(parameters)
Alamofire.request(.POST, url, parameters: parameters as? [String : AnyObject]).responseJSON { (data: Response<AnyObject, NSError>) in
if data.result.isSuccess {
successed(responseObject: data.data)
}else {
failed(error: data.result.error)
}
}
}
// --文件上传
//fileURL实例:let fileURL = NSBundle.mainBundle().URLForResource("Default",withExtension: "png")
func BLUpload(URLString:String,fileURL:NSURL,progress:(bytesWritten: Int64?,totalBytesWritten: Int64?,totalBytesExpectedToWrite: Int64?) -> Void, responseResult:(responseValue: AnyObject?,error: NSError?) -> Void) {
Alamofire.upload(.POST, URLString, file: fileURL).progress {(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) -> Void in
progress(bytesWritten:bytesWritten,totalBytesWritten:totalBytesWritten,totalBytesExpectedToWrite:totalBytesExpectedToWrite)
}.responseJSON { response in
responseResult(responseValue:response.result.value,error:response.result.error)
}
}
/*
** 写法二 block定义成宏的写法
//fileURL实例:let fileURL = NSBundle.mainBundle().URLForResource("Default",withExtension: "png")
func BLUpload(URLString:String,fileURL:NSURL,block:uploadClosure) {
Alamofire.upload(.POST, URLString, file: fileURL).progress {(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) -> Void in
block(nil,nil,bytesWritten ,totalBytesWritten,totalBytesExpectedToWrite)
}.responseJSON { response in
block(response.result.value,response.result.error,nil,nil,nil)
}
}
*/
// --文件下载
//下载到默认路径let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
//默认路径可以设置为空,因为有默认路径
func BLDownload(type:RequestType,URLString:String,progress:(bytesRead: Int64?,totalBytesRead: Int64?,totalBytesExpectedToRead: Int64?) -> Void, responseResult:(responseValue: AnyObject?,error: NSError?) -> Void) {
switch type {
case .requestTypeGet:
Alamofire.download(.GET, URLString, destination: destination)
.progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
progress(bytesRead:bytesRead,totalBytesRead:totalBytesRead,totalBytesExpectedToRead:totalBytesExpectedToRead)
}
.response { (request, response, _, error) in
responseResult(responseValue:response,error:error)
}
break
case .requestTypePost:
Alamofire.download(.POST, URLString, destination: destination)
.progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
progress(bytesRead:bytesRead,totalBytesRead:totalBytesRead,totalBytesExpectedToRead:totalBytesExpectedToRead)
}
.response { (request, response, _, error) in
responseResult(responseValue:response,error:error)
}
}
}
/* block定义成宏的写法
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
//默认路径可以设置为空,因为有默认路径
func BLDownload(type:RequestType,URLString:String,block:uploadClosure) {
switch type {
case .requestTypeGet:
Alamofire.download(.GET, URLString, destination: destination)
.progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
block(nil,nil,bytesRead, totalBytesRead, totalBytesExpectedToRead)
}
.response { (request, response, _, error) in
block(response,error,nil,nil,nil)
}
break
case .requestTypePost:
Alamofire.download(.POST, URLString, destination: destination)
.progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
block(nil,nil,bytesRead, totalBytesRead, totalBytesExpectedToRead)
}
.response { (request, response, _, error) in
block(response,error,nil,nil,nil)
}
}
}
*/
// --上传多张图片
func BLPostUploadMultiPicture(url: String, parameters: AnyObject, imgParameters: [UIImage]?, successed:(responseObject: AnyObject?) -> (), failed: (error: NSError?) -> ()) {
Alamofire.upload(.POST, url, headers: parameters as? [String : String], multipartFormData: { (formData) in
for index in 0..<imgParameters!.count {
let imageData = UIImagePNGRepresentation(imgParameters![index] )
formData.appendBodyPart(data: imageData!, name: "img\(index)", fileName: "\(index).jpg", mimeType: "image/png")
}
}, encodingMemoryThreshold: Manager.MultipartFormDataEncodingMemoryThreshold){ (result) in
switch result {
case .Success(let upload, _, _):
upload.responseJSON{ respone in
print(respone.data)
successed(responseObject: respone.data)
}
case .Failure(let error):
print(error)
break
}
}
}
}
转载请注明出处谢谢 http://my.oschina.net/iceTear/blog/743007
//
// BLHttpSessionsRequest.swift
// ALSFinancial-Swift
//
// Created by 冰泪 on 16/8/23.
// Copyright © 2016年 冰泪. All rights reserved.
//网络请求类封装
import UIKit
import Alamofire
//创建请求类枚举
enum RequestType: Int {
case requestTypeGet
case requestTypePost
}
//创建一个闭包(注:oc中block)
typealias sendVlesClosure = (AnyObject?, NSError?)->Void
typealias uploadClosure = (AnyObject?, NSError?,Int64?,Int64?,Int64?)->Void
class BLHttpSessionsRequest: NSObject {
// --GET请求获取JSON数据
func BLGetJSONDataWithUrl(url: String, parameters: AnyObject, successed:(responseObject: AnyObject?) -> (), failed: (error: NSError?) -> ()) {
Alamofire.request(.GET, url, parameters: parameters as? [String : AnyObject]).responseJSON { (data: Response<AnyObject, NSError>) in
if data.result.isSuccess {
successed(responseObject: data.data)
}else {
failed(error: data.result.error)
}
}
}
// --POST请求获取JSON数据
func BLPostJSONDataWithUrl(url: String, parameters: AnyObject, successed:(responseObject: AnyObject?) -> (), failed: (error: NSError?) -> ()) {
//print(parameters)
Alamofire.request(.POST, url, parameters: parameters as? [String : AnyObject]).responseJSON { (data: Response<AnyObject, NSError>) in
if data.result.isSuccess {
successed(responseObject: data.data)
}else {
failed(error: data.result.error)
}
}
}
// --文件上传
//fileURL实例:let fileURL = NSBundle.mainBundle().URLForResource("Default",withExtension: "png")
func BLUpload(URLString:String,fileURL:NSURL,progress:(bytesWritten: Int64?,totalBytesWritten: Int64?,totalBytesExpectedToWrite: Int64?) -> Void, responseResult:(responseValue: AnyObject?,error: NSError?) -> Void) {
Alamofire.upload(.POST, URLString, file: fileURL).progress {(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) -> Void in
progress(bytesWritten:bytesWritten,totalBytesWritten:totalBytesWritten,totalBytesExpectedToWrite:totalBytesExpectedToWrite)
}.responseJSON { response in
responseResult(responseValue:response.result.value,error:response.result.error)
}
}
/*
** 写法二 block定义成宏的写法
//fileURL实例:let fileURL = NSBundle.mainBundle().URLForResource("Default",withExtension: "png")
func BLUpload(URLString:String,fileURL:NSURL,block:uploadClosure) {
Alamofire.upload(.POST, URLString, file: fileURL).progress {(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) -> Void in
block(nil,nil,bytesWritten ,totalBytesWritten,totalBytesExpectedToWrite)
}.responseJSON { response in
block(response.result.value,response.result.error,nil,nil,nil)
}
}
*/
// --文件下载
//下载到默认路径let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
//默认路径可以设置为空,因为有默认路径
func BLDownload(type:RequestType,URLString:String,progress:(bytesRead: Int64?,totalBytesRead: Int64?,totalBytesExpectedToRead: Int64?) -> Void, responseResult:(responseValue: AnyObject?,error: NSError?) -> Void) {
switch type {
case .requestTypeGet:
Alamofire.download(.GET, URLString, destination: destination)
.progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
progress(bytesRead:bytesRead,totalBytesRead:totalBytesRead,totalBytesExpectedToRead:totalBytesExpectedToRead)
}
.response { (request, response, _, error) in
responseResult(responseValue:response,error:error)
}
break
case .requestTypePost:
Alamofire.download(.POST, URLString, destination: destination)
.progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
progress(bytesRead:bytesRead,totalBytesRead:totalBytesRead,totalBytesExpectedToRead:totalBytesExpectedToRead)
}
.response { (request, response, _, error) in
responseResult(responseValue:response,error:error)
}
}
}
/* block定义成宏的写法
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
//默认路径可以设置为空,因为有默认路径
func BLDownload(type:RequestType,URLString:String,block:uploadClosure) {
switch type {
case .requestTypeGet:
Alamofire.download(.GET, URLString, destination: destination)
.progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
block(nil,nil,bytesRead, totalBytesRead, totalBytesExpectedToRead)
}
.response { (request, response, _, error) in
block(response,error,nil,nil,nil)
}
break
case .requestTypePost:
Alamofire.download(.POST, URLString, destination: destination)
.progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
block(nil,nil,bytesRead, totalBytesRead, totalBytesExpectedToRead)
}
.response { (request, response, _, error) in
block(response,error,nil,nil,nil)
}
}
}
*/
// --上传多张图片
func BLPostUploadMultiPicture(url: String, parameters: AnyObject, imgParameters: [UIImage]?, successed:(responseObject: AnyObject?) -> (), failed: (error: NSError?) -> ()) {
Alamofire.upload(.POST, url, headers: parameters as? [String : String], multipartFormData: { (formData) in
for index in 0..<imgParameters!.count {
let imageData = UIImagePNGRepresentation(imgParameters![index] )
formData.appendBodyPart(data: imageData!, name: "img\(index)", fileName: "\(index).jpg", mimeType: "image/png")
}
}, encodingMemoryThreshold: Manager.MultipartFormDataEncodingMemoryThreshold){ (result) in
switch result {
case .Success(let upload, _, _):
upload.responseJSON{ respone in
print(respone.data)
successed(responseObject: respone.data)
}
case .Failure(let error):
print(error)
break
}
}
}
}
转载请注明出处谢谢 http://my.oschina.net/iceTear/blog/743007
相关文章推荐
- FM音乐播放器 基于豆瓣 api,swift,alamofire,sqlite.swift 等
- SWIFT闭包,介绍,使用(ALAMOFIRE封装 异步请求)
- [绍棠_Swift] Swift3.0中的Alamofire网络请求的封装
- Swift 2.3 【封装网络请求业务类 AFNetworking/ Alamofire】
- Swift - Alamofire - GET/POST/upload 的使用
- swift开发之网络数据库(mysql)的增删改查(2)--引用Alamofire的post传值
- iOS开发--Swift 基于AFNetworking 3.0的网络请求封装
- Swift&Node 使用Alamofire进行Post
- How do I access a nested JSON(嵌套JSON) value using Alamofire and SwiftyJSON
- [快速学会Swift第三方库]AlamofireObjectMapper
- swift Alamofire+ObjectMapper——swift(学习九)
- Swift&NodeJS 使用Alamofire进行Post(zhuan)
- Netty整理和基于Netty封装了一个web game通讯层
- iOS网络请求工具oc版,swift版基于AFNetworking的简单封装
- Swift-->Http网络请求(NSURLSession, Alamofire)
- Swift: Alamofire -> http请求 & ObjectMapper -> 解析JSON
- swift 的网络请求--Alamofire框架
- [Swift 工作tips] 之 使用Alamofire做网络请求时设置请求超时(timeout)时间
- Swift: 用Alamofire做http请求,用ObjectMapper解析JSON
- Swift - 不使用pods如何正确添加类似Alamofire这样的库