views:

175

answers:

1

Hi I am trying to get a cookie from a site which I can do no problem. The problem arises when I try and save the cookie to a NSString in a holder class or anywhere else for that matter and try and access it outside the delegate method where it is first created.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 {
  int i;
  NSString* c;
     NSArray* all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://johncleary.net"]];
  //NSLog(@"RESPONSE HEADERS: \n%@", [response allHeaderFields]);
  for (i=0;i<[all count];i++)
   {
    NSHTTPCookie* cc = [all objectAtIndex: i];
    c = [NSString stringWithFormat: @"%@=%@", [cc name], [cc value]];
    [Cookie setCookie: c];
   NSLog([Cookie cookie]) // Prints the cookie fine.

  }



  [receivedData setLength:0];
 }

I can see and print the cookie when I am in the method but I cant when trying to access it form anywhere else even though it gets stored in the holder class

@interface Cookie : NSObject
{
 NSString* cookie;
}
+ (NSString*) cookie;
+ (void) setCookie: (NSString*) cookieValue;
@end

int main (void)
{
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 JCLogin* login;
 login = [JCLogin new];

 [login DoLogin];
 NSLog([Cookie cookie]); // Crashes the program
 [pool drain];
 return 0;

}
+3  A: 

Does your setCookie: method retain the cookie?

NSString stringWithFormat: returns an autoreleased object, so unless you are retaining it within your setCookie: method, it will go away.

David Gelhar