2010-05-29 48 views
1

我正在構建一個iPhone應用程序,它將允許用戶登錄到驗證用戶並啓動會話的PHP Web服務器。用Cocoa Touch管理用戶的PHP會話

我對管理會話的想法是創建一個具有sharedLogin方法的單例用戶類。將會話變量存儲在共享實例中以維護會話是否審慎?

回答

2

對於那些有興趣的人。我決定的方法是使用NSURLConnection委託方法connection:didReceiveResponse:。然後,我處理了響應標題,並將PHPSESS cookie存儲在單例NSHttpCookieStorage中:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 

    [super connection:connection didReceiveResponse:response]; 
    NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response; 

    NSArray *allCookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[urlResponse allHeaderFields] forURL:[response URL]]; 

    if ([allCookies count]) { 
     [connection cancel]; 

     [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:allCookies forURL:[response URL] mainDocumentURL:nil]; 
    } 
}