import UIKit
class HCNetWork: AFHTTPSessionManager {
//单利
staticletsharedTools:HCNetWork= {
letbaseUrl =NSURL(string:"http://www.51xianchang.com/")!
letmanager =HCNetWork.init(baseURL: baseUrlasURL, sessionConfiguration:URLSessionConfiguration.default)
manager.responseSerializer.acceptableContentTypes = NSSet(objects: "application/json", "text/html", "text/json", "text/javascript", "text/plain") as? Set
// 设置请求参数为JSON字符串
manager.requestSerializer = AFJSONRequestSerializer()
// 请求超时设定
manager.requestSerializer.timeoutInterval = 10
// 设置允许自生成证书
manager.securityPolicy.allowInvalidCertificates = true
//设置请求头
manager.requestSerializer.setValue("APP", forHTTPHeaderField:"loginType")
manager.requestSerializer.setValue("iOS", forHTTPHeaderField:"deviceType")
returnmanager
}()
typealiasrequestBack = (_response:Any?,_error:Error?) -> ()
}
extension HCNetWork {
funcrequestDictionary(urlString:String, parameters: [String:AnyObject]?, finished:@escapingrequestBack) {
letsuccess = { (task:URLSessionDataTask?, responseObject:Any?) -> ()in
finished(responseObject, nil)
}
letfailure = { (task:URLSessionDataTask?, error:Error) -> ()in
finished(nil, error)
}
post(urlString, parameters: parameters, progress:nil, success: success, failure: failure)
}
}
拿走可用
网友评论