Hello,
I'm literally a newbie to iPhone Development. Amongst all the other wonders, I'm trying to POST username & password to an 'already LIVE' webpage which, upon correct credentials will return a Welcome message for me. The bit I'm left wondering about is that there's no call to NSURLConnection delegate method didReceiveData. I've checked (using NSLog) other delegate methods like didReceiveResponse, connectionDidFinishLoading and they are being called also there's no call being made to didFailWithError, which makes me assume that there's no error occurring in the process. Following is my code, I'd be grateful if anyone could point me in the right direction.
Code:
- (IBAction)UpdateLabel:(id)sender
{
BOOL flag = YES;
self.username = textField.text;
self.password = passField.text;
NSString *uname = self.username;
NSString *pass = self.password;
if([uname length] == 0)
{
uname = @"Enter something for Christmas's sake!";
flag = NO;
}
if ([pass length] == 0)
{
pass = @"Where's the password mate ?!";
flag = NO;
}
if(flag)
{
NSString *post = [[NSString alloc] initWithFormat:@"Username=%@&Password=%@", uname, pass];
NSMutableData *postData = [NSMutableData data];
[postData appendData:[post stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURL URLWithString:@"http://www.2sms.com/iPhoneApp.aspx"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSString *displayURL = [[NSString alloc] initWithFormat:@"%@%@", url, post];
label.text = displayURL;
finished = NO;
//I have also tried synchronous request, only to no avail.
/*
NSError *error;
NSHTTPURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
*/
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection)
{
while (!finished)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
receivedData = [[NSMutableData data] retain];
}
@try
{
//NSString *returnData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
label.text = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"%@", [label text]);
}
@catch (NSException *e)
{
}
@finally
{
[post release];
[displayURL release];
}
}
}
...and NSURLConnection Delegate Methods
Code:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"From ReceivedResponse");
[self.receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"From ReceiveData");
if(self.receivedData == nil)
{
self.receivedData = [[NSMutableData alloc] initWithLength:2048];
}
[self.receivedData appendData:data];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"%d bytes of data", [receivedData length]);
finished = YES;
[receivedData release];
[connection release];
}