Rails 3.0 iPhone session
I create a session passing user id and password to a rails 3.0 server.
In- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)response
I get the allHeaderFields dictionary from the response and it has a "Set-Cookie" value.
"Set-Cookie" = "_quest_svr_session=BAh7ByIPc2Vzc2lvbl9pZCIlNDRmOTUyZmI2OTZlZTIzMDdlZDg2ZmI5ZTZhM2E4YTA6E3JlbWVtYmVyX3Rva2VuWwdpBiJFMDE1ZjE5OWJkMzU2YzMwNzE2ZjAxMTVhMzI4OTI0MzY3ODU0OTJmZTIyZmE3ZDFmZjdhM2ZiMDdlODFhZWZjMA%3D%3D--38aa7c663ccf53ddd39a0ded89c6f87da5b2a开发者_运维问答bad; path=/; HttpOnly";
I store the value in
NSString * session_token =[dictionary objectForKey:@"Set-Cookie"];
When I create an NSMutableURLRequest I set the cookie value to the store session token
[theRequest setValue:session_token forHTTPHeaderField:@"Cookie"];
The problem is that the rails server is doing a redirect to the sign in page in the rails controller I use
before_filter :authenticate, :except => [ :new, :create, :templates]
You shouldn't have to handle this manually. From the docs:
The URL loading system automatically sends any stored cookies appropriate for an NSURLRequest. unless the request specifies not to send cookies. Likewise, cookies returned in an NSURLResponse are accepted in accordance with the current cookie acceptance policy.
NSMutableURLRequest also has the method
- (void)setHTTPShouldHandleCookies:(BOOL)handleCookies
But the default value is YES
, so again you shouldn't have to do anything.
精彩评论