我開發,其中服務器遞給我一個cookie來識別用戶的應用程序不堅持餅乾。共享NSHTTPCookieStorage實例
我連續請求需要有一個cookie來具有用戶所期望的響應。 我無法理解的是NSHTTPCookieStorage的共享實例如何以及何時丟失了其Cookie。
我使用的第一個解決方案是存檔和從我的服務器餅乾保存到應用終端從我的應用程序啓動服務器,然後清除現有的用戶默認設置和重置我儲存的人。 通過開發過程,我沒有遇到問題,因爲調試會話非常短,通常不需要將應用程序放在後臺。
在beta測試期間,麻煩開始了。 我附帶的破解是不僅在應用程序終止時保存cookie,而且在將這些cookie遞給我的API調用之後也保存這些cookie。並且不僅在應用程序啓動時加載保存的Cookie,而且在應用程序返回到前臺時也加載。
NSHTTPCookieStorage共享實例如何擺脫這些cookie以及處理它的最佳做法是什麼,因爲它是我的應用程序的重要組成部分,如果沒有經驗豐富的開發人員備份,我不能相信這樣的黑客解決方案。
預先感謝您爲您的回答
編輯: 這裏有保存方法/讀/清除餅乾
-(void)saveStoredCookies
{
NSURL *httpUrl = @"http://myServer.com";
NSURL *httpsUrl = @"https://myServer.com";
NSArray *httpCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpUrl];
NSData *httpCookiesData = [NSKeyedArchiver archivedDataWithRootObject:httpCookies];
[[NSUserDefaults standardUserDefaults] setObject:httpCookiesData forKey:@"savedHttpCookies"];
NSArray *httpsCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpsUrl];
NSData *httpsCookiesData = [NSKeyedArchiver archivedDataWithRootObject:httpsCookies];
[[NSUserDefaults standardUserDefaults] setObject:httpsCookiesData forKey:@"savedHttpsCookies"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
-(void)readStoredCookies
{
//clear, read and install stored cookies
NSURL *httpUrl = @"http://myServer.com";
NSURL *httpsUrl = @"https://myServer.com";
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpUrl];
for (NSHTTPCookie *cookie in cookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpsUrl];
for (NSHTTPCookie *cookie in cookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
NSData *httpCookiesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"savedHttpCookies"];
if([httpCookiesData length]) {
NSArray *savedCookies = [NSKeyedUnarchiver unarchiveObjectWithData:httpCookiesData];
for (NSHTTPCookie *cookie in savedCookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
}
NSData *httpsCookiesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"savedHttpsCookies"];
if([httpsCookiesData length]) {
NSArray *savedCookies = [NSKeyedUnarchiver unarchiveObjectWithData:httpsCookiesData];
for (NSHTTPCookie *cookie in savedCookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
}
}
-(void)clearStoredCookies
{
NSURL *httpUrl = @"http://myServer.com";
NSURL *httpsUrl = @"https://myServer.com";
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpUrl];
for (NSHTTPCookie *cookie in cookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpsUrl];
for (NSHTTPCookie *cookie in cookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
請根據您使用的代碼進行更新,以便在發出新請求時將Cookie從Cookie存儲區中取出。你的描述是不夠的。 – 2012-08-09 03:23:50
您沒有看到問題,因爲您無法在測試期間在新會話之間加載來自存儲的Cookie。 – 2012-08-09 03:27:15