11

I am new to iphone development.I have posted the URL with the user-name and password. I am able to print the data in "connection didReceiveData " method.But i see "connection didReceiveData" method called twice.I don't know ,where i am going wrong. Here is my code

 - (void)viewDidLoad {
[super viewDidLoad];

NSString *post = [NSString stringWithFormat:@"&domain=school.edu&userType=2&referrer=http://apps.school.edu/navigator/index.jsp&username=%@&password=%@",@"xxxxxxx",@"xxxxxx"];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://secure.school.edu/login/process.do"]]];

[request setHTTPMethod:@"POST"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];

[request setHTTPBody:postData];

NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

if(conn)
{
    NSLog(@"Connection Successful");

}
else
{
    NSLog(@"Connection could not be made");
}

    }

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data{

NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"the  data %@",string);
  }

The whole HTML page is printed twice in the console.So please help me out.Thanks.

Warrior
  • 39,156
  • 44
  • 139
  • 214

2 Answers2

14

You may receive the response data in chunks, which is why NSURLConnection's documentation states:

"The delegate should concatenate the contents of each data object delivered to build up the complete data for a URL load."

Use an instance of NSMutableData for this and only process the complete data once you receive the -connectionDidFinishLoading: message.

Rohit Mandiwal
  • 10,258
  • 5
  • 70
  • 83
Alex Repty
  • 1,060
  • 1
  • 9
  • 17
  • If you receive large amounts of data, you may want to look at NSFileHandler to write the chunks to the disk as they arrive. Otherwise NSMutableData should be fine, especially if you do not want to store the data anyway. – Felix Lamouroux Mar 15 '10 at 10:37
  • Good point, Felix - especially regarding the iPhone's limited memory. – Alex Repty Mar 15 '10 at 14:02
11

As MacOS Developer Library states, connection:didReceiveData can be called multiple times if data is received in chunks. That means you have to save all the chunks in some variable and do data processing in connectionDidFinishLoading method. e.g.

NSMutableData *receivedData = [[NSMutableData alloc] init];

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    [receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data, for example log:
    NSLog(@"data: %@", [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]
}
ULazdins
  • 1,975
  • 4
  • 25
  • 31