I have this Alamofire request like so:
func loginUser(_ username: String, password: String, completion: @escaping (_ result: Bool) -> Void)
{
let user = username
let password = password
let url = webservice
let credential = URLCredential(user: user, password: password, persistence: .none)
let headers = ["Accept": "application/json;odata=verbose", "Content-type": "application/json;odata=verbose"]
Alamofire.request(url, method: .get, headers: headers).authenticate(usingCredential: credential).responseJSON {
(response) in
print(response.result)
switch response.result {
case .success:
if let value = response.result.value {
completion(true)
}else{
print("There is error in the server response")
completion(false)
}
case .failure (let error):
print("The NTLM request error is: ", error.localizedDescription)
completion(false)
}
}
}
My problem with it is it works when I use invalid credentials after using valid credentials, instead of spitting out an error. I have been told this maybe a caching issue, so my question is how do I disable cache for this request?
I have tried adding this to the top of my request:
URLCache.shared.removeAllCachedResponses()
But that didn't work
I also saw this:
urlRequest.cachePolicy = .reloadIgnoringCacheData
But I have no idea where to apply it.
Please help!
UPDATE
I have tried the following:
func loginUser(_ username: String, password: String, completion: @escaping (_ result: Bool) -> Void)
{
let user = username
let password = password
let url = webservice
let credential = URLCredential(user: user, password: password, persistence: .none)
var urlRequest = URLRequest(url: URL(string: url)!)
urlRequest.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
urlRequest.httpMethod = "get"
urlRequest.setValue("application/json;odata=verbose", forHTTPHeaderField: "Content-type")
urlRequest.setValue("application/json;odata=verbose", forHTTPHeaderField: "Accept")
Alamofire.request(urlRequest).authenticate(usingCredential: credential).responseJSON {
(response) in
switch response.result {
case .success:
if let value = response.result.value {
completion(true)
}else{
print("There is error in the server response")
completion(false)
}
case .failure (let error):
print("The NTLM request error is: ", error.localizedDescription)
completion(false)
}
}
}
But that did not work, same result.
You can instantiate a Alamofire.SessionManager
with a specific URLSessionConfiguration
. In the configuration you can set the cachePolicy. This could look like this:
let configuration = URLSessionConfiguration.default
configuration.requestCachePolicy = .useProtocolCachePolicy // or whatever you want
configuration.urlCache = nil
let mySessionManager = Alamofire.SessionManager(configuration: configuration)
and then use it as follows:
mySessionManager.request(urlRequest)
....
Note that you have to use mySessionManager
to do requests instead of using Alamofire.request
if you follow this.
To make a request using mySessionManager it is basically the same instead of Alamofire.request(urlRequest)
... you use mySessionManager.request(urlRequest)
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.