开发者

Managing a user's PHP session with Cocoa Touch

I'm building an iPhone app, which will allow users to log in to a PHP web server that 开发者_如何学Cauthenticates the user and starts a session.

My idea for managing a session is to create a singleton User class that has a sharedLogin method. Would it be prudent to store the session variable in the shared instance in order to maintain the session?


For those interested. The method I decided on is to use NSURLConnection delegate method connection:didReceiveResponse:. Then I processed the response headers and store the PHPSESS cookie in the singleton 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];
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜