NsurlConnection使应用程序使用iOS X崩溃



我想在我的应用中提出一个POST请求。我使用以下代码:

NSString *post = [NSString stringWithFormat:@"Email=%@&Password=%@",self.emailTxt.text,self.pwTxt.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:kEmailLogin]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *responseCode = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];

当我使用iOS 9设备的模拟器运行此操作时,它可以很好地工作。但是在我的iPad中,每次执行请求时,它都会崩溃。

我应该如何执行POST请求以使其在iOS X设备中工作?

kEmailLogin是一个变量,用于返回JSON的服务的HTTPS URL。

我已经使用 NSURLSession解决了此问题。

NSString *post = [NSString stringWithFormat:@"Email=%@&Password=%@",self.emailTxt.text,self.pwTxt.text];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL *url = [NSURL URLWithString:kEmailLogin];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPBody = [post dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPMethod = @"POST";
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;  
}];
[postDataTask resume];

最新更新