我想做一个登录申请。我的第一个屏幕登录表单有两个文本字段和一个按钮。当我按下这个按钮时,我会调用一个调用此方法的方法:
- (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",但不知道如何修复它。
我还有一个问题。如何在下一个屏幕中使用此cookies?
谢谢
发布于 2011-05-11 14:56:25
使用[request release];,您将释放一个自动释放的对象。不要这样做,它将由自动释放池在下一个运行循环周期中释放。在初始化结束时删除自动释放,这样就可以了,否则删除release语句。
您可以在这里创建它:
request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://server.com/login.php"]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60] autorelease]; // <---https://stackoverflow.com/questions/5960211
复制相似问题