Alamofire 网络请求封装
Xcode新建项目:AlamofireDemo
打开终端:cd 进入项目文件夹
运行: pod init 新建podfile文件
用xcode打开 podfile
platform :ios,'9.0'
use_frameworks!
target'AlamofireDemo'do
pod 'Alamofire',
end
继续打开项目目录的终端:运行 pod install --no-repo-update
不需要更新本地库 --no-repo-update

打开测试网络请求的网站:http://httpbin.org/
创建网络工具类:新建文件,Cocoa Touch Class

继承:NSObject

import UIKit
import Alamofire
//定义请求参数的类型
enumMethodType{
caseget
casepost
}
classNetworkTools {
//1.第一个传入的参数的,网络请求请求方式,2,请求的网络地址,3.请求的参数,字符串数组,4.回调的闭包方法
classfuncrequestData(_type:MethodType, URLString:String, parameters: [String:Any]?
=nil, finishedCallback:@escaping(_result:Any)->()){
//1.获取网络请求的方式
letmethod = type == .get?HTTPMethod.get:HTTPMethod.post
//2.发送网络请求
Alamofire.request(URLString, method: method, parameters: parameters).responseJSON{(response)in
//3.获取结果
guardletresult = response.result.valueelse{
print(response.result.error)
return
}
//4.将结果回调回去
finishedCallback(result)
} } }
设置支持发送http数据请求:

