In this application we will see how to receive data from the server. This is the simple login application and when you login in the screen it will display the message, whatever we set in the server page. So let see how it will be worked.

Step 1: Create a View base application using template. Give the application name “SendReceiveData”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Xpand classes and notice Interface Builder created the SendReceiveDataViewController class for you. Expand Resources and notice the template generated a separate nib, SendReceiveDatayViewController.xib, for the “SendReceiveData”.

Step 4: In the SendReceiveDataViewController.h file, we have added UITextField, UILabel, and one buttonClick method. So make the following changes in the file.

#import <UIKit/UIKit.h>

@interface SendReceiveDataViewController :
UIViewController<UITextFieldDelegate> {

        IBOutlet UITextField *nameInput;
        IBOutlet UITextField *passInput;
        IBOutlet UILabel *greeting;
        NSMutableData *webData;
       
}

@property(nonatomic, retain) IBOutlet UITextField *nameInput;
@property(nonatomic, retain) IBOutlet UITextField *passInput;
@property(nonatomic, retain) IBOutlet UILabel *greeting;
@property(nonatomic, retain) NSMutableData *webData;

-(IBAction)buttonClick: (id) sender;

Step 5: Double click the SendReceiveDataViewController.xib file and open it to the Interface Builder. Drag three labels, one Round Rect Button and two TextField from the library and place it to the view window(See the figure below). Connect File’s Owner icon to the view icon and select view and drag File’s Owner icon to the “Input Your Values” and select greeting . Next drag File’s Owner icon to the first text field and select nameInput,do the same thing for the next text field and select passInput. Select the login button and bring up Connection Inspector and drag TouchUpInside to the File’s Owner icon and select buttonClick: action. Now save the SendReceiveDataViewController.xib file, close it and go back to the Xcode.

Step 6: Open the SendReceiveDataViewController.m file and make the following changes in the file:

-(IBAction)buttonClick:(id)sender
{
       
        NSString* username = nameInput.text;
        NSString* pass = passInput.text;
       
        if([nameInput.text isEqualToString:@"" ]|| [passInput.text isEqualToString:@""])
        {
               
                greeting.text = @"Input Your Value";
                [nameInput resignFirstResponder];
                [passInput resignFirstResponder];
                return;
        }
       
        NSString *post =
        [[NSString alloc] initWithFormat:@"uname=%@&pwd=%@",username,pass];
       
        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
       
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];  
       
        NSURL *url = [NSURL URLWithString:@"http://www.chakrainteractive.com/mob/iphone/login/chckusr.php"];
        NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
        [theRequest setHTTPMethod:@"POST"];  
        [theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];  
        [theRequest setHTTPBody:postData];     
       
       
        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
       
        if( theConnection )
        {
                webData = [[NSMutableData data] retain];
        }
        else
        {
               
        }
       
        [nameInput resignFirstResponder];
        [passInput resignFirstResponder];
        nameInput.text = nil;
        passInput.text = nil;
       
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
        [webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
       
       
        [webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
       
        [connection release];
        [webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
       
        NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
        NSLog(loginStatus);
        greeting.text = loginStatus;
        [loginStatus release];
       
       
       
        [connection release];
        [webData release];
}

Step 7: Now compile and run the application in the Simulator.

You can Download SourceCode from here SendReceiveData-1