如何发送asynchronousURL请求?

我想知道如何获得一个返回值1或0只是…从一个URL请求asynchronous返回。

目前我是这样做的:

NSString *UTCString = [NSString stringWithFormat:@"http://web.blah.net/question/CheckQuestions?utc=%0.f",[lastUTCDate timeIntervalSince1970]]; NSLog(@"UTC String %@",UTCString); NSURL *updateDataURL = [NSURL URLWithString:UTCString]; NSString *checkValue = [NSString stringWithContentsOfURL:updateDataURL encoding:NSASCIIStringEncoding error:Nil]; NSLog(@"check Value %@",checkValue); 

这工作,但是它阻止我的主线程,直到我从URL的回复,我如何设置它,这样做会在另一个线程,而不是主线程?

编辑:答案我结束upcalling我的function,这个效果不错:)

 [self performSelectorInBackground:@selector(shouldCheckForUpdate) withObject:nil]; 

你可以使用NSURLConnection

 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; 

并使用其委托方法处理其响应和错误。

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error - (void)connectionDidFinishLoading:(NSURLConnection *)connection 

你可以findNSURLConnection的实现

  • 苹果文档:使用NSURLConnection
  • 如何通过示例使用iOS NSURLConnection

编辑:虽然NSURLConnection是由苹果提供更多的build议的方式放置URL请求。 但是,我发现AFNetworking库非常省时,易于实现,而且作为第三方实现也非常简单。 你应该试试看。

尝试这个 :

。H:

 NSMutableData *responseData; 

.M:

 - (void)load { NSURL *myURL = [NSURL URLWithString:@"http://www.example.com"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { responseData = [[NSMutableData alloc] init]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [responseData appendData:data]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [responseData release]; [connection release]; [textView setString:@"Unable to fetch data"]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"Succeeded! Received %d bytes of data",[responseData length]); NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease]; } 

使用NSURLConnection并提出请求。 然后你可以用NSURLConnection的方法启动同步或asynchronous连接:

同步加载数据

 + sendSynchronousRequest:returningResponse:error: 

asynchronous加载数据

 + connectionWithRequest:delegate: – initWithRequest:delegate: – initWithRequest:delegate:startImmediately: – start 

检查Apple Developer API参考中的NSURLConnection类。

无耻地从https://gist.github.com/knmshk/3027474复制。; 所有学分转到https://gist.github.com/knmshk

 xmlData = [[NSMutableData alloc] init]; NSURL *url = [NSURL URLWithString: @"http://forrums.bignerdranch.com/smartfeed.php?" @"limit=NO_LIMIT&count_limit20&sort_by=standard&" @"feed_type=RSS2.0&feed_style=COMPACT"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; //connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; NSOperationQueue *queue = [[NSOperationQueue alloc]init]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){ if (error) { xmlData = nil; NSLog(@"error:%@", error.localizedDescription); } [xmlData appendData:data]; }]; 

iOS XCode文档中有一个名为LazyTableImages的示例。 在滚动停止之后,这将执行asynchronousURL以及asynchronous图像加载到屏幕上显示的UITableView单元格。 协议的优秀例子,asynchronous数据处理等