gpt4 book ai didi

ios - NSHTTPCookieStorage 丢失 cookie,如果应用程序在重启后手机至少解锁一次之前启动

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:27 26 4
gpt4 key购买 nike

问题

正如标题所说,我目前遇到一个问题,即 NSHTTPCookieStorage 在手机重启后至少解锁一次之前在后台初始化时丢失我们应用程序的 cookie。

一旦 cookie 丢失,它们将无法以任何方式恢复,迫使用户重新登录以检索一组新的 cookie 并恢复 session 。

如果应用事件是在用户解锁她/他的手机后首次注册的,那么一切都会正常进行。

上下文:

  • 我们使用 NSURLRequestNSURLSession 时也发生了这个问题使用 ASIHTTP 和 AFNetworking 自动处理 cookie,所以我们得出的结论是它影响了整个 NSHTTPCookieStorage类。

  • 我们的应用程序具有 SLC(重大位置变化监控),因此它
    在后台自动触发。

重现步骤

  1. 制作一个执行网络调用的演示应用程序(“让我们称之为 CALLA"), 向服务器进行身份验证,获取一些 cookie 作为响应对此。
  2. 执行第二次调用(我们称它为“CALL B”)需要这个要发送的 cookie 才能工作。如果一切顺利, cookies 应自动管理并相应地发送到服务器。
  3. 让您的演示应用程序能够执行一些后台操作,例如示例启用 SLC 监控。在执行这个背景行为再次执行“CALL B”(如果它在后台任务中与否对于测试目的并不重要)
  4. 重启手机,不要解锁。
  5. 等到 SLC,或者你选择的背景行为是触发并完成 CALL B。

    提示:您可以禁用和启用飞行模式以在设备上强制触发 SLC。

    问题:如果在此期间触发了重大的位置更改,应用程序将永远丢失所有 cookie,没有任何机会以任何方式恢复它们。

任何帮助或想法将不胜感激。

最佳答案

似乎 iOS 有 this same issue但 cookie 存储在 NSHTTPCookieStorage 上。

我已经创建了一个雷达,所以可以随意添加更多,这样这个问题就会在队列中被推高一点。雷达编号为16237165。

与此同时,您可以执行以下解决方法:

  1. 手动处理 cookie(也就是不自动依赖 iOS管理 cookie 并将它们存储在 NSHTTPCookieStorage 中类(class))。但是不要使用NSUSerDefaults 因为它有同样的问题。

  2. 在手机至少解锁一次之前阻止所有网络事件。这是我们选择的选项,我们执行了以下操作:

    • 使用 NSFileProtectionCompleteUntilFirstUserAuthentication 权限在磁盘中创建一个文件。
    • 在进行任何网络调用之前,请检查该应用是否能够读取该文件。如果是,则意味着手机至少解锁过一次,因此您可以在不丢失 cookie 的情况下调用电话。

这是一个代码示例:

static BOOL phoneIsUnlocked = NO;

//If this var is true, then avoid re checking if file has permissions (Cause if it was granted permissions once, it will have access now)
if(phoneIsUnlocked) return phoneIsUnlocked;

//If phone has never been unlocked, prevent all networking stuff just to make sure cookies are not lost due to an ios7 bug.
//Creates a file with NSFileProtectionCompleteUntilFirstUserAuthentication permissions. If the app is able to read it, it means the phone was unlocked at least once after a reboot.

//Get the file path
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"a.secure"];

//create file if it doesn't exist
if(![[NSFileManager defaultManager] fileExistsAtPath:fileName])
[[NSFileManager defaultManager] createFileAtPath:fileName
contents:[@"secure" dataUsingEncoding:NSUTF8StringEncoding]
attributes:[NSDictionary dictionaryWithObject:NSFileProtectionCompleteUntilFirstUserAuthentication
forKey:NSFileProtectionKey]];

NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:fileName];
phoneIsUnlocked = file != nil; //If file is not nil, the phone has been unlocked
[file closeFile];

关于ios - NSHTTPCookieStorage 丢失 cookie,如果应用程序在重启后手机至少解锁一次之前启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22646726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com