0

i have login screen for company registration, i want to post all uitextfield data on web service on button clicked .

company registration image

this is my code for button clicked

- (IBAction)Register:(id)sender {

if (_CompanyName.text.length <=0)
{
    NSLog(@"enter company name");
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Enter Company Name"
                                                      message:nil
                                                     delegate:self
                                            cancelButtonTitle:@"Cancel"
                                            otherButtonTitles:@"Continue", nil];
    [message setAlertViewStyle:UIAlertViewStyleDefault];

    [message show];
}

I am beginners in that type of activity so,if anyone have knowledge then please help,thanks.

2 Answers2

1

Very simple posting data to server coding is below

- (IBAction)actionRegister:(id)sender
{

  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:strYourURL]];

  [request setHTTPMethod:@"POST"];

  //Check The Value what we entered in textField
  NSLog(@"the company name is:%@",txtFieldCompanyName.text);
  NSLog(@"the email is:%@",txtFieldEmail.text);
  NSLog(@"the password is:%@",txtFieldPassword.text);
  NSLog(@"the password again is:%@",txtFieldPasswordAgain.text);


  //Pass The String to server
  NSString *strParameters =[NSString stringWithFormat:@"comapny_name=%@&email=%@&password=%@&password_again=%@",txtFieldCompanyName.text,txtFieldEmail.text,txtFieldPassword.text,txtFieldPasswordAgain.text,nil];

  //Check The Value what we passed
  NSLog(@"the data Details is =%@", strParameters);

  //Convert the String to Data
  NSData *data1 = [strParameters dataUsingEncoding:NSUTF8StringEncoding];

  //Apply the data to the body
  [request setHTTPBody:data1];

  //Create the response and Error
  NSError *err;
  NSURLResponse *response;

  NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

  NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];

  //This is for Response
  NSLog(@"got response==%@", resSrt);

  if(resSrt)
  {
    NSLog(@"got response");
  }
  else
  {
    NSLog(@"faield to connect");
  }
}
user3182143
  • 9,459
  • 3
  • 32
  • 39
0

Use AFNetworking class Here is a link

https://github.com/AFNetworking/AFNetworking

http://www.raywenderlich.com/59255/afnetworking-2-0-tutorial

And some peace of code you will get from this tutorial like

- (IBAction)registerButtonTapped:(id)sender
{
    // 1
    NSString *string = [NSString stringWithFormat:@"www.yourwebapiurl.php?format=json&username=%@", userNameTextField.text];//and so on
    NSURL *url = [NSURL URLWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];


// 2
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    // 3
    NSDictionary *responseDict = (NSDictionary *)responseObject;
    NSLog(@"You have got the response ");

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    // 4
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
}];

// 5
[operation start];
}

It will Help.Thanks

baydi
  • 1,003
  • 6
  • 11