如何在unbutton上发布web服务中的uitextfield数据

时间:2015-10-27 09:29:29

标签: ios json

我有公司注册的登录屏幕,我想在点击的按钮上发布所有uitextfield数据。

company registration image

这是我点击按钮的代码

- (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];
}

我是这类活动的初学者,所以,如果有人有知识,那么请帮助,谢谢。

2 个答案:

答案 0 :(得分:1)

非常简单地将数据发布到服务器编码

- (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");
  }
}

答案 1 :(得分:0)

使用AFNetworking类这是一个链接

https://github.com/AFNetworking/AFNetworking

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

您将从本教程中获得一些代码安静,如

- (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];
}

它会帮助。谢谢