0
我的應用程序給我錯誤的主要功能。我試圖讓NSURLConnection同步。我如何在主同步中創建此代碼? 這裏是代碼(在支持文件的main.m):如何使UIApplicationMain同步
#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");
}
}
我不明白你的意思是'主同步'? 'UIApplicationMain()'本身就是一個阻塞調用。你也可以發佈錯誤? – Amar
添加一個異常斷點併發布崩潰的堆棧跟蹤。 – Mar0ux
我用ARC編寫了viewDidLoad中的代碼,但我不想讓它自動釋放。所以我必須改變@autoreleasepool。它是真的還是沒有影響? –