問題描述
我正在開發(fā)一個應(yīng)用程序,在該應(yīng)用程序中服務(wù)器將 cookie 交給我來識別用戶.
I'm developing an application where the server hands me a cookie to identify the user.
我的連續(xù)請求需要該 cookie 才能獲得用戶期望的響應(yīng).我無法理解的是 NSHTTPCookieStorage 的共享實例如何以及何時丟失其 cookie.
My successive requests need to have that cookie to have the response that the user expects. What I can't understand is how and when the shared instance of NSHTTPCookieStorage loses its cookies.
我使用的第一個解決方案是在應(yīng)用程序終止時將 cookie 從我的服務(wù)器存檔并保存到用戶默認(rèn)值,然后在應(yīng)用程序啟動時從我的服務(wù)器中清除現(xiàn)有的 cookie,并重置我存儲的那些.在開發(fā)過程中,我沒有遇到任何問題,因為調(diào)試會話非常短,通常不需要將應(yīng)用程序置于后臺.
The first solution I used is to archive and save the cookies from my server to user defaults on application terminations then clear existing ones from my server on application launch and reset the ones I stored. Through the developing process I didn't encounter problems as the sessions for debugging are very short and didn't usually need to put the app in background.
在 Beta 測試期間,問題開始了.我?guī)淼募记墒遣粌H在應(yīng)用程序終止時保存 cookie,而且在將這些 cookie 交還給我的 API 調(diào)用之后保存.并且不僅在應(yīng)用啟動時加載保存的 cookie,而且在應(yīng)用返回前臺時加載.
During beta testing, troubles started. The hack I came with is to save the cookies not only on application termination but also after the API calls that hand me back these cookies. And to load the saved cookies not only on app launch but also when the app returns to foreground.
NSHTTPCookieStorage 共享實例如何擺脫這些 cookie 以及處理它的最佳實踐是什么,因為它是我的應(yīng)用程序的重要組成部分,如果沒有更有經(jīng)驗的開發(fā)人員支持,我無法相信這種被黑的解決方案.
How come the NSHTTPCookieStorage share instance gets rid of these cookies and what's the best practice to deal with it as it's a vital part of my app and I can't trust such a hacked solution if not backed up by more experienced developers.
提前感謝您的回答
以下是保存/讀取/清除cookies的方法
Here are the methods to save/read/clear the cookies
-(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];
}
}
推薦答案
解決了!
經(jīng)過一些簡單的調(diào)試后,我發(fā)現(xiàn)問題出在我在 cookiesForURL:
中使用的 url.我只是開始使用 cookies
屬性,現(xiàn)在它工作正常.
After some simple debugging I found out that the problem was with the url I was using in cookiesForURL:
.
I simply started using cookies
property and now it works fine.
這篇關(guān)于NSHTTPCookieStorage 的共享實例不保留 cookie的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!