Sunday, January 2, 2011

Creating a POST request and downloading the data. - iOS SDK


@implementation NetCommunicator

@synthesize myFeedConnection,myReceivedData;

-(void)createPostRequestWith:(NSString *)username andPassword:(NSString *)password {

NSString *post = [NSString stringWithFormat:@"username=%@&password=%@",username,password];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

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

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@""]];

[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

self.myFeedConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];

// Test the validity of the connection object. The most likely reason for the connection object
// to be nil is a malformed URL, which is a programmatic error easily detected during development
// If the URL is more dynamic, then you should implement a more flexible validation technique, and
// be able to both recover from errors and communicate problems to the user in an unobtrusive manner.

NSAssert(self.myFeedConnection != nil, @"Failure to create URL connection.");

// show in the status bar that network activity is starting
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

#pragma mark -
#pragma mark NSURL Connection Delegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

if (connection == self.myFeedConnection) {
self.myReceivedData = [NSMutableData data];


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

if (connection == self.myFeedConnection) {

[self.myReceivedData appendData:data];


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

if (connection == self.myFeedConnection) {
if ([error code] == kCFURLErrorNotConnectedToInternet)
// if we can identify the error, we can present a more precise message to the user.
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"No Connection Error"
NSError *noConnectionError = [NSError errorWithDomain:NSCocoaErrorDomain
[self handleError:noConnectionError];
// otherwise handle the error generically
[self handleError:error];
// release our connection
self.myFeedConnection = nil;


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

if(connection == self.myFeedConnection) {

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

NSLog(@"my Data: %.*s", [self.myReceivedData length], [self.myReceivedData bytes]);

// Do the rest with the downloaded data.

self.myFeedConnection = nil;



// -------------------------------------------------------------------------------
// handleError:error // This method should be modified ..
// -------------------------------------------------------------------------------
- (void)handleError:(NSError *)error
NSString *errorMessage = [error localizedDescription];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Cannot download"
[alertView show];
[alertView release];


No comments:

Post a Comment