I already have successfully got keychain for my token and passing it to AccessTokenAdapter class shown below. http127.0.0.1:8000/api2/projects/?format=json is passed as proj
When you make sessionManager a let constant it won't live longer than the embracing it function, thus the session ends as soon as the manager is deallocated.
To fix this, make sessionManager live longer. For example in my case I made it a class property:
class NetworkRequest: {
var sessionManager = Alamofire.SessionManager()
...
func performRequest(_ call: APICall, customLink: String = "", parameters: Dictionary = ["":"" as Any]) {
let sessionConfig = URLSessionConfiguration.default
sessionConfig.timeoutIntervalForRequest = call.suggestedTimeout()
sessionConfig.timeoutIntervalForResource = call.suggestedTimeout()
sessionManager = Alamofire.SessionManager(configuration: sessionConfig)
sessionManager.adapter = AccessTokenAdapter()
sessionManager.request(urlString,
method: call.method(),
parameters: parameters,
encoding: call.chooseEncoding(),
headers: [:])
.responseJSON
{ response in
...
}
}
The solution might be different according to your situation, but the idea is to keep sessionManager alive until the network request ends.