Saturday, 19 May 2012

Username and Password authentication..how to retrive data from server in iphone sdk


 //////////////////// in .h file //////////

IBOutlet UITextField *txtUser;
    IBOutlet UITextField *txtPass;
    IBOutlet UILabel *lblMsg;
    NSMutableData *webData; 

 \\\\\\\\\\\\\\\\\\\\  in . m file  ////////////////////////////////

-(IBAction)btnLoginClicked:(id)sender{
    NSString *strUserName = txtUser.text;
    NSString *strPassword = txtPass.text;
   
    if ([strUserName isEqualToString:@""] || [strPassword isEqualToString:@""]) {
        lblMsg.text = @"Input your Value";
        [txtPass resignFirstResponder];
        [txtUser resignFirstResponder];
    }
   
    NSString *post = [[NSString alloc] initWithFormat:@"log=%@&pwd=%@",strUserName,strPassword];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"",[postData length]];
    NSURL *url = [NSURL URLWithString:@"http://ideaact.neonexusdemo.com/wp-content/themes/idea/andr.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 {
       
    }
   
    [txtUser resignFirstResponder];
    [txtPass resignFirstResponder];
    txtUser.text = nil;
    txtPass.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);
    lblMsg.text = loginStatus;
    [loginStatus release];
   
   
   
    [connection release];
    [webData release];
}

No comments:

Post a Comment