Alamofire是在URLSession
和URL加载系统的基础上写的。所以,为了更好地学习这个框架,建议先熟悉下列几个底层网络协议栈:
- URL Loading System Programming Guide >>
- URLSession Class Reference >>
- URLCache Class Reference >>
- URLAuthenticationChallenge Class Reference >>
Session Manager
高级别的方便的方法,例如Alamofire.request
,使用的是默认的Alamofire.SessionManager
,并且这个SessionManager是用默认URLSessionConfiguration
配置的。
例如,下面两个语句是等价的:
Alamofire.request("https://httpbin.org/get") let sessionManager = Alamofire.SessionManager.default sessionManager.request("https://httpbin.org/get")
我们可以自己创建后台会话和短暂会话的session manager,还可以自定义默认的会话配置来创建新的session manager,例如修改默认的header httpAdditionalHeaders
和timeoutIntervalForRequest
。
用默认的会话配置创建一个Session Manager
let configuration = URLSessionConfiguration.default let sessionManager = Alamofire.SessionManager(configuration: configuration)
用后台会话配置创建一个Session Manager
let configuration = URLSessionConfiguration.background(withIdentifier: "com.example.app.background") let sessionManager = Alamofire.SessionManager(configuration: configuration)
用默短暂会话配置创建一个Session Manager
let configuration = URLSessionConfiguration.ephemeral let sessionManager = Alamofire.SessionManager(configuration: configuration)
修改会话配置
var defaultHeaders = Alamofire.SessionManager.defaultHTTPHeaders defaultHeaders["DNT"] = "1 (Do Not Track Enabled)" let configuration = URLSessionConfiguration.default configuration.httpAdditionalHeaders = defaultHeaders let sessionManager = Alamofire.SessionManager(configuration: configuration)
注意:不推荐在Authorization
或者Content-Type
header使用。而应该使用Alamofire.request
API、URLRequestConvertible
和ParameterEncoding
的headers参数。
会话代理
默认情况下,一个SessionManager
实例创建一个SessionDelegate
对象来处理底层URLSession
生成的不同类型的代理回调。每个代理方法的实现处理常见的情况。然后,高级用户可能由于各种原因需要重写默认功能。
重写闭包
第一种自定义SessionDelegate
的方法是通过重写闭包。我们可以在每个闭包重写SessionDelegate
API对应的实现。下面是重写闭包的示例:
/// 重写URLSessionDelegate的`urlSession(_:didReceive:completionHandler:)`方法 open var sessionDidReceiveChallenge: ((URLSession, URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?))? /// 重写URLSessionDelegate的`urlSessionDidFinishEvents(forBackgroundURLSession:)`方法 open var sessionDidFinishEventsForBackgroundURLSession: ((URLSession) -> Void)? /// 重写URLSessionTaskDelegate的`urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)`方法 open var taskWillPerformHTTPRedirection: ((URLSession, URLSessionTask, HTTPURLResponse, URLRequest) -> URLRequest?)? /// 重写URLSessionDataDelegate的`urlSession(_:dataTask:willCacheResponse:completionHandler:)`方法 open var dataTaskWillCacheResponse: ((URLSession, URLSessionDataTask, CachedURLResponse) -> CachedURLResponse?)?
下面的示例演示了如何使用taskWillPerformHTTPRedirection
来避免回调到任何apple.com
域名。
let sessionManager = Alamofire.SessionManager(configuration: URLSessionConfiguration.default) let delegate: Alamofire.SessionDelegate = sessionManager.delegate delegate.taskWillPerformHTTPRedirection = { session, task, response, request in var finalRequest = request if let originalRequest = task.originalRequest, let urlString = originalRequest.url?.urlString, urlString.contains("apple.com"