问题
正如标题所说,我目前遇到的问题是,NSHTTPCookieStorage
在重启后至少解锁一次手机之前,在后台初始化时,我们的应用会丢失Cookie。
一旦cookie丢失,就无法以任何方式恢复,迫使用户重新登录以检索一组新的cookie并恢复会话。
如果在用户解锁手机后第一次注册了应用活动,那么一切都像魅力一样。
上下文
我们也使用NSURLRequest
和NSURLSession
来解决此问题
如同使用ASIHTTP和AFNetworking以及自动cookie处理一样
我们得出结论它影响整个NSHTTPCookieStorage
类。
我们的应用程序有SLC(重要的位置变更监控)所以其中 在后台自动触发。
重现的步骤
等到SLC或您选择的背景行为 触发和CALL B完成。
提示:您可以禁用并启用飞行模式以强制设备上的SLC触发器。
问题:如果在那段时间内触发了重要的位置更改,应用程序将永久丢失所有Cookie,而无法以任何方式恢复它们。
任何帮助或想法都将受到赞赏。
答案 0 :(得分:1)
似乎iOS正在使用this same issue但是存储在NSHTTPCookieStorage上的Cookie。
我已经创建了一个雷达,所以可以随意添加更多,所以这个问题在队列中被推高了一点。雷达编号为16237165。
同时,您可以执行以下解决方法:
手动处理cookie(也就是说不自动依赖iOS)
管理cookie并将其存储在NSHTTPCookieStorage
中
类)。但不要使用NSUSerDefaults
,因为它有。{
同样的问题。
在手机至少解锁一次之前阻止所有网络活动。这是我们采取的选择,我们做了以下工作:
NSFileProtectionCompleteUntilFirstUserAuthentication
权限在磁盘中创建文件。以下是代码示例:
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];
答案 1 :(得分:0)
根据您描述的行为,NSHTTPCookieStorage
使用的基础文件存储上的保护似乎已设置为"Complete until first login"。
由于您无法更改NSHTTPCookieStorage
存储数据的位置和方式,因此您有以下几种选择:
UIApplication
的{{3}}属性并推迟更新,直到设备解锁。您可以注册设备解锁时建议的protectedDataAvailable
通知。