禁用缓存Alamofire iOS [英] Disable Cache Alamofire iOS

查看:209
本文介绍了禁用缓存Alamofire iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Alamofire发出iOS请求时禁用缓存。当我尝试向服务器发出请求,然后在以其他用户身份验证时发出请求时,我会得到304状态代码。

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.

后端使用Express.js处理请求,并使用Passport.js进行用户身份验证。

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

是正确的禁用Alamofire和iOS应用程序上的缓存的方法?还是有什么办法可以防止后端发生这种情况?无论哪种方式,我都不确定下一步的下一步。

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.

更新

在仔细研究了此问题之后,即使传递到身份验证部分的信息完全不同,Alamofire仍在发送完全相同的身份验证信息。但是,如果我等待几分钟,然后再次尝试请求,它将更新为正确的身份验证信息。即使您在新的请求中更新身份验证信息,也几乎会缓存几分钟。因此,直到每隔几分钟左右清除一次身份验证信息,您才能更改身份验证信息。

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.

更新2

下面是我的代码

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)
        }
    }
}

更新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)"]

因此,在使用上面的最新代码来处理HTTP身份验证之后,我仍然遇到问题。在iOS设备上打印 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.

推荐答案

如果要完全禁用Alamofire中的缓存,则必须创建不包含<$ c $的自定义管理器c> urlCache

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(...)

这篇关于禁用缓存Alamofire iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆