首页 > 解决方案 > 不具有 GroupContainerIdentifier 的持久性 HTTPCookieStorage

问题描述

我设置了一个HTTPCookieStorage这样的:

let storage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: "user100")
storage.cookieAcceptPolicy = .always
let cookieProperties: [HTTPCookiePropertyKey : Any] = [.name : "example\(Date().timeIntervalSince1970)",
         .value : "value\(Date().timeIntervalSince1970)",
            .domain : "www.example\([100,200,300].randomElement()!).com",
         .originURL : "www.example.com",
         .path : "/",
         .version : "0",
         .expires : Date().addingTimeInterval(2629743)
        ]

storage.setCookie(HTTPCookie(properties: cookieProperties)!)

我发现这样做HTTPCookieStorage.shared实际上可以保存 cookie,但这种习惯HTTPCookieStorage不是。如何让它持久化?

标签: swiftuikithttpcookienshttpcookiestorage

解决方案


这是我的发现,forGroupContainerIdentifiercookie 的目的是在您的应用程序之间共享 cookie。就像在一个应用程序中创建一个用于 cookie 存储的组,而在另一个应用程序中,您希望访问该组,因此为此您需要选择正确的组名称。您需要在应用程序门户开发人员站点上创建组,并且需要将该组添加到您的两个应用程序包 ID 中。之后,您将能够使用这些 cookie。有关更多信息,请查看此线程。Cookie 存储


推荐阅读