我的应用程序在主要功能中给我错误。我试图使 NSURLConnection 同步。如何使此代码处于主同步状态?这是代码(支持文件中的 main.m):
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
NSUrlConnection 所在的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *soapMessage = [NSString stringWithFormat:@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><m:GetRoomList xmlns:m=\"http://http://port number/WebService.svc/\"><departId xsi:type=\"xsd:int\">0</departId><roomID xsi:type=\"xsd:int\">0</roomID></m:GetRoomList></SOAP-ENV:Body></SOAP-ENV:Envelope>"];
NSURL *url=[NSURL URLWithString:@"http://port number/WebService.svc/"];
NSMutableURLRequest *theRequest= [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/WebService.svc" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest addValue:@"port number" forHTTPHeaderField:@"Host"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLResponse* response = nil;
NSData* data =[NSURLConnection sendSynchronousRequest: theRequest returningResponse: &response error: nil];
//self.theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
if (data) {
NSLog(@"%@",data);
} else {
NSLog(@"Failed");
}
}