如何将cookie值存储为String?

时间:2010-06-11 15:38:07

标签: iphone objective-c cookies

我在将cookie值存储为iPhone项目的字符串时遇到问题。目前,我正在以这种方式检索cookie的值:

NSString *somevalue;

for (NSHTTPCookie *cookie in cookies)
{
    NSLog(@"Name: %@ : Value: %@, Expires: %@", cookie.name, cookie.value, cookie.expiresDate);
    somevalue = cookie.value;
}

现在,理论上这应该将cookie.value存储为字符串'somevalue'。我可以做一个'somevalue'的NSLog,它会检索正确的值。

然而,当我把它传递给:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:fullUrl]];
[request setPostValue:someValue forKey:@"profile_id"];
[request setPostValue:@"1" forKey:@"start"];
[request setPostValue:@"10" forKey:@"end"];

然后检查响应,它传入整个cookie(这就是它的样子):

a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%221fcd876dc0240ac4a59f3630b8f630fe%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A7%3A%220.0.0.0%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A50%3A%22Doubletake+1.0+%28iPhone+Simulator%3B+iPhone+OS+3.1.3%3B%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221276270293%22%3Bs%3A5%3A%22email%22%3Bs%3A18%3A%22tim%40domain.com%22%3B%7D5f9c567d5832013183d511dba8910cf3

值实际存储了吗?为什么会这样?

2 个答案:

答案 0 :(得分:0)

如果您希望将对象保持在当前方法调用之后(或者通常只是超过其所有者的生命周期),则需要使用retain声明对其的所有权。有关完整说明,请参阅memory management rules

答案 1 :(得分:0)

NSString *somevalue;

for (NSHTTPCookie *cookie in cookies)
{
    NSLog(@"Name: %@ : Value: %@, Expires: %@", cookie.name, cookie.value, cookie.expiresDate);
    somevalue = cookie.value;
}