2

我想做登录应用程序。我的第一个屏幕登录表单有两个文本字段和一个按钮。当我按下此按钮时,我会调用一个调用此方法的方法:

- (int)login {
// Add data to post request

NSHTTPURLResponse * response;
NSString *myRequestString = [[NSString alloc] initWithFormat:@"userdata='%@'&passdata='%@'",
                             username.text, password.text];
NSError * error;
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request;
request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://server.com/login.php"]
                                        cachePolicy:NSURLRequestReloadIgnoringCacheData 
                                    timeoutInterval:60] autorelease];

[request setHTTPMethod: @"POST"];
[request setHTTPBody: myRequestData];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];

NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];  

NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://server.com/login.php"]];

//int cid;

for (NSHTTPCookie *cookie in all) {
    NSLog(@"Name: %@ : Value: %@", cookie.name, cookie.value); 
//    cid = (int)cookie.value;
}

//  NSLog(@"id: %d",cid);

[myRequestString release];
[request release];

return 1;
}

当我按下此按钮时,我的程序崩溃并在此行旁边:

NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://sms.britecs.com/login.php"]];

我有Thread1: Program received signal: "EXC_BAD_ACCESS"但不知道如何解决它。

我还有一个问题。如何在下一个屏幕中使用此 cookie?

谢谢

4

1 回答 1

2

随着[request release];您释放一个自动释放的对象。不要那样做,它会在下一个运行循环周期被自动释放池释放。在初始化结束时删除 autorelease 就可以了,否则删除 release 语句。

在这里你正在创建它:

request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://server.com/login.php"]
                                        cachePolicy:NSURLRequestReloadIgnoringCacheData 
                                    timeoutInterval:60] autorelease]; // <---
于 2011-05-11T06:56:25.157 回答