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.
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;
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 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];
}
{
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];
}
No comments:
Post a Comment