1,首先使用cocoapods导入
2,直接上代码:
a 先看下文件结构
CommonFile.swift
import UIKit
let HGL_ScreenHeight = UIScreen.main.bounds.size.height
let HGL_ScreenWidth = UIScreen.main.bounds.size.width
NetWorkTools.swift
import UIKit
import Alamofire
enum MethodType:NSInteger {
case GET
case POST
}
class NetWorkTools: NSObject {
class func requestData(_ type : MethodType, URLString : String, parameters : [String : Any]? = nil, finishedCallback : @escaping (_ result : Any) -> ()) {
//1.获取类型
let method = type == .GET ? HTTPMethod.get : HTTPMethod.post
// 2.发送网络请求
Alamofire.request(URLString, method: method, parameters: parameters).responseJSON { (response) in
// 3.获取结果
guard let result = response.result.value else {
print(response.result.error!)
return
}
// 4.将结果回调出去
finishedCallback(result)
}
}
}
3,ViewController.swift:
NetWorkTools.requestData(.POST, URLString: "http:///appapi/api/gethelps", parameters: [" category_id":"10"]) { (result) in
print(result)
}
打印如下: