Disable Cache Alamofire iOS

2019-05-21 18:22发布

I'm trying to disable caching when making iOS requests using Alamofire. When I try to make a request to the server and then make a request while authenticated as a different user I get a 304 status code back.

I've tried everything at this link, all of which don't work or result in errors.

The backend is using Express.js to handle requests and Passport.js for user auth.

Is the correct approach to disable caching on Alamofire and on the iOS application? Or is there something I can do to prevent this happening on the backend? Either way I'm not quite sure next steps for how to move forward.

Update

After looking into this issue a little bit more it looks like Alamofire is sending the exact same authentication information even tho the information getting passed into the authentication section is completely different. But if I wait a few minutes and try the request again it updates to the correct auth information. It's almost like the auth information is cached for a few minutes even if you update it in a new request. So you can't change the auth information until it clears out every few minutes or so. Which makes even less sense.

Update 2

Below is my code

func reloadData() {
    print("Reloading data!!")
    let headers: HTTPHeaders = [
        "testvalidationtoken": "test",
        ]

    let keychain = KeychainSwift()

    let email: String = keychain.get("email")!
    let password: String = keychain.get("password")!


    var configuration = URLSessionConfiguration()
    configuration.urlCache = nil
    var manager : SessionManager = SessionManager(configuration: configuration)


    manager.request("http://IPHERE:3000/api/path", headers: headers).authenticate(user: email, password: password).validate().responseJSON { response in
        switch response.result {

        case .success(let value):

            let json = JSON(value)
            print("JSON: \(json)")

            for item in json.array! {

                let title: String? = item["title"].stringValue
                self.titles.append(title!)
                self.colors.append(UIColor.blue)
            }

            self.tableView.reloadData()

        case .failure(let error):
            print ("My Error")
            print (error)
            let alertController = UIAlertController(title: "Error", message: "Error, please try again", preferredStyle: UIAlertControllerStyle.alert)
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
            }
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
}

Update 3

let loginString = String(format: "%@:%@", email, password)
let loginData = loginString.data(using: String.Encoding.utf8)!
let base64LoginString = loginData.base64EncodedString()

print(base64LoginString)

let headers: HTTPHeaders = ["Authorization": "Basic \(base64LoginString)"]

So after using this latest code above to handle HTTP auth I'm still running into problems. When printing base64LoginString on the iOS device it returns one value then when using those headers to send the request to the server authorization = the previous value.

2条回答
男人必须洒脱
2楼-- · 2019-05-21 18:46

you can try this:

let cstorage = HTTPCookieStorage.shared
if let cookies = cstorage.cookies(for: url) {
    for cookie in cookies {
        cstorage.deleteCookie(cookie)
    }
}

hope this will help you

查看更多
做自己的国王
3楼-- · 2019-05-21 19:04

If you want to completely disable cache in Alamofire you have to create custom manager without urlCache

var configuration = URLSessionConfiguration.default
configuration.urlCache = nil
configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
var manager : SessionManager = SessionManager(configuration: configuration)

manager.request(...)
查看更多
登录 后发表回答