【问题标题】:How to use setHTTPBody from UITextField input如何从 UITextField 输入中使用 setHTTPBody
【发布时间】:2015-10-12 13:16:42
【问题描述】:

我正在使用 mycontacts RESTful API 集成到我的应用程序中。现在,我想使用 UITextField 而不是手动输入的用户名和密码。我不知道,如何在 setHTTPBody 中做到这一点。有什么建议吗?

NSURL *URL = [NSURL URLWithString:@"https://api.addressbook.io/v1/login"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"POST"];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

//Here I want to use username and password obtained by UITextField.
[request setHTTPBody:[@"{\n  \"username\": \"Enter username\",\n  \"password\": \"EnterPassword\",\n  \"client\": \"apiary\"\n}" dataUsingEncoding:NSUTF8StringEncoding]];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                        completionHandler:
                              ^(NSData *data, NSURLResponse *response, NSError *error) {

                                  if (error) {
                                      // Handle error...
                                      return;
                                  }

                                  if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
                                      NSLog(@"Response HTTP Status code: %ld\n", (long)[(NSHTTPURLResponse *)response statusCode]);
                                      NSLog(@"Response HTTP Headers:\n%@\n", [(NSHTTPURLResponse *)response allHeaderFields]);
                                  }

                                  NSString* body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                  NSLog(@"Response Body:\n%@\n", body);
                              }];
[task resume];

【问题讨论】:

    标签: ios xcode ios7 ios8 ios6


    【解决方案1】:

    我猜你不想让按钮点击应该调用哪个 web 服务。您可以做的是为 UItextField 设置委托并使用此委托方法

    - (void)textFieldDidEndEditing:(UITextField *)textField;
    

    此委托方法的作用是每次您移出文本字段时都会调用它。在这里您可以检查用户是否输入了文本字段并设置了您的 http 正文。

    【讨论】:

    • [request setHTTPBody:[@"{\n \"username\": \"Enter username\",\n \"password\": \"EnterPassword\",\n \"client \": \"apiary\"\n}" dataUsingEncoding:NSUTF8StringEncoding]];我已经实现了这些代表和一切。我只想知道,我怎样才能使用上面那一行,因为上面那一行是字符串体。
    【解决方案2】:

    使用 NSString 并使用文本字段中的用户名和密码对其进行格式化,并将该正文发送到 setHTTPbody。

    NSURL *URL = [NSURL URLWithString:@"https://api.addressbook.io/v1/login"];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
    [request setHTTPMethod:@"POST"];
    
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    //Improved code lines
    NSString *username = ((UITextField*)[self.view viewWithTag:1]).text;
    NSString *password = ((UITextField*)[self.view viewWithTag:2]).text;
    
    NSString *body = [NSString stringWithFormat:@"{\n  \"username\": \"%@\",\n  \"password\": \"%@\",\n  \"client\": \"apiary\"\n}",username,password];
    
    [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
     //Improved code lines end
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                            completionHandler:
                                  ^(NSData *data, NSURLResponse *response, NSError *error) {
    
                                      if (error) {
    
                                          [self alert:@"Error" message:@"Check network connection"];
    
                                          return;
                                      }
    
                                      if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
                                          NSLog(@"Response HTTP Status code: %ld\n", (long)[(NSHTTPURLResponse *)response statusCode]);
    
                                          }
                                          NSLog(@"Response HTTP Headers:\n%@\n", [(NSHTTPURLResponse *)response allHeaderFields]);
                                      }
    
                                      NSString* body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                      NSLog(@"Response Body:\n%@\n", body);
                                  }];
    [self.view endEditing:YES];
    [task resume];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      相关资源
      最近更新 更多