objective-c - NSUrlConnection 使应用程序崩溃 iOS X
objective-c - NSUrlConnection makes the app crash with 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 和 iOS X 中,每次我执行请求时它都会崩溃。
我应该如何执行 POST
请求以使其在 iOS X 设备上工作?
kEmailLogin
是具有 HTTPS
url 的变量,用于 returns 和 JSON
.
的服务
我已经使用 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];
我想在我的应用程序中发出 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 和 iOS X 中,每次我执行请求时它都会崩溃。
我应该如何执行 POST
请求以使其在 iOS X 设备上工作?
kEmailLogin
是具有 HTTPS
url 的变量,用于 returns 和 JSON
.
我已经使用 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];