2010-05-13 171 views
0

嗨我想從一個站點獲取cookie,我可以做任何問題。當我嘗試將Cookie保存到持有者類或其他任何地方的NSString中並嘗試在第一次創建的委託方法之外嘗試訪問它時,就會出現問題。在[NSURLConnection sendSynchronousRequest]中回調後無法訪問NString

- (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]; 
} 

我可以看到並打印餅乾,當我的方法,但我不能試圖訪問其形成其他地方,即使它被存儲在holder類

@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; 

} 
+1

首先,不要直接記錄字符串,總是使用'NSLog(@「%@」,xxx)'。如果一個字符串包含一個有效的格式說明符,則會損壞堆棧。 – dreamlax 2010-05-14 00:44:24

回答

3

setCookie:時方法retain的cookie?

NSString stringWithFormat:返回一個自動釋放對象,所以除非您保留在您的setCookie:方法中,否則它將消失。

相關問題