JSON数据网络模块
import UIKit
import AFNetworking
import MBProgressHUD
import Toast
struct NetManager {
static func GET(URLString:String,parameters:[String:NSObject]?,showHUD:Bool = true,success:((NSObject?) -> Void)?,failure:((NSError) -> Void)?){//传递URL为字符串类型,参数为字典类型,默认加载进度条,成功可选必包和失败的必包
let manager = AFHTTPSessionManager()
manager.requestSerializer.timeoutInterval = 10 //超时时间为10s
let mainWindow = UIApplication.sharedApplication().delegate?.window! //把窗体主window别名 mianWindow
if showHUD { //loading条淡入淡出的效果
MBProgressHUD.showHUDAddedTo(mainWindow, animated: true)
}
manager.GET(URLString, parameters: parameters, success: { //调用GET方法,参数为上面定义的parameters
(task,responObject) -> Void in
if showHUD {
MBProgressHUD.hideAllHUDsForView(mainWindow, animated: true)//隐藏loading条
}
success?(responObject as? NSObject) //执行必包
}) { (task, error) -> Void in
if showHUD {
MBProgressHUD.hideAllHUDsForView(mainWindow, animated: true)
mainWindow?.makeToast(“网路异常”)//toast提示
}
failure?(error)
}
}
static func POST(URLString:String,parameters:[String:NSObject]?,showHUD:Bool = true,success:((NSObject?) -> Void)?,failure:((NSError) -> Void)?){
let manager = AFHTTPSessionManager()
manager.requestSerializer.timeoutInterval = 10
let mainWindow = UIApplication.sharedApplication().delegate?.window!
if showHUD {
MBProgressHUD.showHUDAddedTo(mainWindow, animated: true)
}
manager.POST(URLString, parameters: parameters, success: {
(task,responObject) -> Void in
if showHUD {
MBProgressHUD.hideAllHUDsForView(mainWindow, animated: true)
}
success?(responObject as? NSObject)
}) { (task, error) -> Void in
if showHUD {
MBProgressHUD.hideAllHUDsForView(mainWindow, animated: true)
mainWindow?.makeToast(“网路异常”)
}
failure?(error)
}
}
}