更新UIWebView Cookie的到期日期

时间:2016-09-25 15:13:43

标签: ios swift cookies

我正在我的应用中显示一个网页登录页面。我不希望用户在登录后再次登录。但是我认为登录后的Cookie会在8小时后过期。我可以更新此cookie的到期日期吗?下面是我得到cookie的代码 -

if let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies {
            for cookie in cookies {
                NSLog("Cookiee name  \(cookie.name)  \n" )
                NSLog("Cookiee value  \(cookie.value) \n")
                NSLog("Cookiee domain  \(cookie.domain) \n")
                NSLog("Cookiee expiresDate \(cookie.expiresDate) \n")
                NSLog("Cookiee path  \(cookie.path) \n")
                NSLog("Cookiee comment \(cookie.comment) \n")
                NSLog("Cookiee commentURL \(cookie.commentURL) \n\n\n")
            }
        }

从此为auth cookie返回的到期日期是2016-09-25 15:09:51 +0000。如何将其更新为无限?

1 个答案:

答案 0 :(得分:0)

如果您无法在网站的源代码中设置Cookie过期,则可以手动执行。要使用更新的到期间隔检索和设置Cookie,您可以遵循此方法。 Swift 3.0 提供更短的代码:

let storage = HTTPCookieStorage.shared
        for cookie in storage.cookies! {

            // catch a cookie info

            storage.deleteCookie(cookie)

            // set a new cookie with update expiration here using catched info
    }

更多文档:https://developer.apple.com/reference/foundation/httpcookiestorage