2
votes

I want to make login application. My first screen login form with two text fields and one button. When I pres this button I invoke one method that invoke this method:

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

When I pres this button my program crash and next to this row:

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

i have Thread1: Program received signal: "EXC_BAD_ACCESS" but don't know how to fix it.

I have one more question. How can I use this cookies in my next screen?

Thanks

1
What does the console say when you enable NSZombieEnabled for your active executable?Nick Weaver
have a look at this small tutorial.Nick Weaver
Thanks @Nick Weaver :) [request release]; cause the error, but don't know why.nyanev

1 Answers

2
votes

With [request release]; you are releasing an autoreleased object. Don't do that, it'll be released the next run loop cycle by the autorelease pool. Remove the autorelease at the end of the initialization and you are fine, else remove the release statement.

Here you are creating it:

request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://server.com/login.php"]
                                        cachePolicy:NSURLRequestReloadIgnoringCacheData 
                                    timeoutInterval:60] autorelease]; // <---