由于Alamofire是swift网络库,所以,以下的所有介绍均基于swift项目
导入Alamofire
以下为使用cocoapods导入,其余的方式请参考官网
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks! # 这句一定不要忘了
target '<Your Target Name>' do
pod 'Alamofire', '~> 4.5'
end
将Alamofire导入到项目中之后,在所需要用到的地方使用以下方式导入
import Alamofire
创建普通接口数据请求对象(下载和上传与之类似)
/// 请求的URL,需要遵循URLConvertible协议,字符串即可
let requestUrl = "http://www.baidu.com"
/**
网络请求可以选择的方式:
public enum HTTPMethod: String {
case options = "OPTIONS"
case get = "GET"
case head = "HEAD"
case post = "POST"
case put = "PUT"
case patch = "PATCH"
case delete = "DELETE"
case trace = "TRACE"
case connect = "CONNECT"
}
*/
let requestMethod: HTTPMethod = .get
/// 请求的参数,[String: Any]类型的数据,可以为nil
let requestParameters: Parameters? = nil
/**
参数的编码方式
default
methodDependent /// default
queryString
httpBody
*/
let requestEncoding: ParameterEncoding = URLEncoding.default
/// 请求的headers,[String: String]类型,可以为nil
let requestHeaders: HTTPHeaders? = nil
/// 创建一个数据请求对象
let dataRequest = request(requestUrl, method: requestMethod, parameters: requestParameters, encoding: requestEncoding, headers: requestHeaders)
/// 取消网络请求
dataRequest.cancel()
/// 暂停网络请求
dataRequest.suspend()
/// 重新开始网络请求
dataRequest.resume()
发起网络请求并拿到指定格式的数据
/// 返回值为json格式
dataRequest.responseJSON { (response: DataResponse) in }
/// 返回的数据为json格式。在指定的线程中进行请求,并且指定数据解析时的选项
dataRequest.responseJSON(queue: DispatchQueue.main, options: .mutableContainers) { (response: DataResponse) in }
/// 返回值为Data格式
dataRequest.responseData { (response: DataResponse) in }
/// 返回值为Data格式。在指定的线程中进行请求
dataRequest.responseData(queue: DispatchQueue.main) { (response: DataResponse) in }
/// 返回值为String格式
dataRequest.responseString { (response: DataResponse) in }
/// 返回值为String格式。在指定的线程中进行请求,并且指定字符串的编码格式
dataRequest.responseString(queue: DispatchQueue.main, encoding: String.Encoding.utf8) { (response: DataResponse) in }
网络状态相关
/// 创建网络状态管理对象
let reachabilityManager = NetworkReachabilityManager.init()
/// 获取网络是否正常
let isReachable = reachabilityManager?.isReachable
/// 获取移动网络是否正常
let isReachableOnWWAN = reachabilityManager?.isReachableOnWWAN
/// 获取wifi是否正常
let isReachableOnEthernetOrWiFi = reachabilityManager?.isReachableOnEthernetOrWiFi
/// 设置网络监听所在的线程
reachabilityManager?.listenerQueue = DispatchQueue.main
/// 网络状态发生改变时的闭包回调
reachabilityManager?.listener = { (state: NetworkReachabilityManager.NetworkReachabilityStatus) in }
/// 启动网络状态的监听
reachabilityManager?.startListening()
/// 停止网络状态的监听
reachabilityManager?.stopListening()
网络请求相关通知
/// 当请求被取消时会发送这个通知
NotificationCenter.default.addObserver(self, selector: #selector(didCancel), name: NSNotification.Name.Task.DidCancel, object: nil)
/// 当请求完成是会发送这个通知
NotificationCenter.default.addObserver(self, selector: #selector(didComplete), name: NSNotification.Name.Task.DidComplete, object: nil)
/// 当请求继续或者重新开始的时候会发送这个通知
NotificationCenter.default.addObserver(self, selector: #selector(didResume), name: NSNotification.Name.Task.DidResume, object: nil)
/// 当请求被暂停时会发送这个通知
NotificationCenter.default.addObserver(self, selector: #selector(didSuspend), name: NSNotification.Name.Task.DidSuspend, object: nil)