How to implement network interceptor in alamofire?
From Alamofire 4.0+, we can use the Custom Session Manager.
Today we’ll talk about Request Adapter and Request Retirer.
Request Adapter: A type that can adapt a URLRequest in some manner if necessary. For example, we can add token or version code in header for every request
Request Retirer: A type that determines whether a request should be retried when error occurred. For example, we can refresh token when 401 status code returned or retry request as many you want or do nothing.
Let’s dive into it!
First, create custom alamofire session with network interceptor.
Second, customize network interceptor
Add custom header in adapt method, and implement retry in retry method!
Finally, use session in every api calls
Don’t forget if you want to retry request, request should raise error!
You can do this by .validate() on request.
If request get failure of token expired, refresh token and retry request on retry.
We can check this by status code 401 (unauthorized).
For more details,
'개발 > swift' 카테고리의 다른 글
How to get app store version in swift? (0) | 2020.10.13 |
---|---|
Swift Random Generator (0) | 2020.10.13 |
Multiple link UILabel in swift (0) | 2020.10.11 |
Swift navigation controller freeze on swipe gesture (0) | 2020.10.07 |
Swift IBInspectable and IBDesignable (0) | 2020.10.06 |