所以我希望我的应用程序在发送 http 请求并获得响应时不要锁定 GUI,我尝试了,但它抱怨我在主线程之外使用 uikit,有人可以告诉我分离 http 和桂?
-(void)parseCode:(NSString*)title{
UIActivityIndicatorView *spinner;
spinner.center = theDelegate.window.center;
spinner.tag = 12;
[theDelegate.window addSubview:spinner];
[spinner startAnimating];
dispatch_queue_t netQueue = dispatch_queue_create("com.david.netqueue", 0);
dispatch_async(netQueue, ^{
NSString *url =[NSString stringWithFormat:@"http://myWebService.org/"];
// Setup request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded"];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableString *data = [[NSMutableString alloc] init];
[data appendFormat:@"lang=%@", @"English"];
[data appendFormat:@"&code=%@", theDelegate.myView.text ];
[data appendFormat:@"&private=True" ];
[request setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]];
NSHTTPURLResponse *urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&urlResponse
error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
[spinner stopAnimating];
[spinner removeFromSuperview];
[self presentResults:result];
});
});
}