0
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"riderfinder.appspot.com/login"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"text/plain" 
   forHTTPHeaderField:@"Content-type"];

    NSString *body = @"username=";
    [body stringByAppendingString:accountEntered];
    [body stringByAppendingString:@"&"];
    [body stringByAppendingString:@"password="];
    [body stringByAppendingString:passwordEntered];

    NSMutableData *data = [[NSMutableData data] initWithString:body];

    //Crashes everything with "SIGABRT" warning/error. Nothing else is said.
    [request setHTTPBody:data];

如果有人知道出了什么问题,我将不胜感激。我通过 Apple 的调试器将其缩小到导致崩溃的最后一行。非常感谢!

4

2 回答 2

1

有2个错误:

  1. 正文没有附加, [body stringByAppendingString:accountEntered]应该是body=[body stringByAppendingString:accountEntered]

  2. NSMutableData *data = [[NSMutableData data] initWithString:body];使用不正确,可以使用NSData *data=[body dataUsingEncoding:NSUTF8StringEncoding];

所以我修改了代码:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"riderfinder.appspot.com/login"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"text/plain" forHTTPHeaderField:@"Content-type"];

NSString *body = @"username=";
body=[body stringByAppendingString:accountEntered];
body=[body stringByAppendingString:@"&"];
body=[body stringByAppendingString:@"password="];
body=[body stringByAppendingString: passwordEntered];

//NSMutableData *data = [[NSMutableData data] initWithString:body];
NSData *data=[body dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPBody:data];
于 2012-09-10T01:48:41.330 回答
0

首先,你应该让 'accountEntered' 和 'passwordEntered' 不为零。如果你已经这样做了,你应该设置断点来确切地知道哪一行崩溃。通常,“SIGABRT”错误意味着你释放了更多或无法识别的选择器。答案 1 上的代码是正确的。

于 2012-09-10T03:08:18.387 回答