swift基于Alamofire的简易封装整理

swift基于Alamofire的简易封装整理,第1张

概述// //  BLHttpSessionsRequest.swift //  ALSFinancial-Swift // //  Created by 冰泪 on 16/8/23. //  Copyright © 2016年 冰泪. All rights reserved. //网络请求类封装 import UIKit import Alamofire   //创建请求类枚举 enum Reque

//
// 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?)->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,Failed: (error: NSError?) -> ()) {
//print(parameters)
Alamofire.request(.POST,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",block:uploadClosure) {

Alamofire.upload(.POST,totalBytesExpectedToWrite) -> VoID in
block(nil,nil,bytesWritten,totalBytesExpectedToWrite)
}.responseJsON { response in
block(response.result.value,response.result.error,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,error: NSError?) -> VoID) {
switch type {
case .requestTypeGet:
Alamofire.download(.GET,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,error:error)
}
}
}

/* block定义成宏的写法

let destination = Alamofire.Request.suggestedDownloadDestination(directory: .documentDirectory,block:uploadClosure) {
switch type {
case .requestTypeGet:
Alamofire.download(.GET,totalBytesExpectedToRead) in

block(nil,bytesRead,totalBytesExpectedToRead)
}
.response { (request,error) in
block(response,error,nil)
}
break
case .requestTypePost:
Alamofire.download(.POST,totalBytesExpectedToRead) in
block(nil,nil)
}
}
}

*/

// --上传多张图片
func BLPostUploadMultiPicture(url: String,imgParameters: [UIImage]?,Failed: (error: NSError?) -> ()) {
Alamofire.upload(.POST,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

总结

以上是内存溢出为你收集整理的swift基于Alamofire的简易封装整理全部内容,希望文章能够帮你解决swift基于Alamofire的简易封装整理所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1072830.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存