import UIKit
import AFNetworking
enum RequestType : String{
case GET = "GET"
case POST = "POST"
}
class NetworkTools: AFHTTPSessionManager {
//let 是线程安全的
static let shareInstance : NetworkTools = {
let tools = NetworkTools()
tools.responseSerializer.acceptableContentTypes?.insert("text/html")
return tools
}()
}
//MARK:- 封装请求方法
extension NetworkTools{
func require(methodType:RequestType,urlString:String,parameters:[String:Any] ,finished:@escaping (Any?,Error?)->()){
// 定义成功的回调闭包
let successCallBack = { (task: URLSessionDataTask, responseObj: Any?) in
finished(responseObj as? [String : Any], nil)
}
// 定义失败的回调闭包
let failureCallback = { (task: URLSessionDataTask?, error: Error) in
finished(nil, error )
}
if methodType == .GET{
get(urlString, parameters: parameters, progress: nil, success: successCallBack, failure: failureCallback)
}else{
post(urlString, parameters: parameters, progress: nil, success: successCallBack, failure: failureCallback)
}
}
}