Iphone 如何发送异步URL请求?

Iphone 如何发送异步URL请求?,iphone,ios,xcode,nsurlconnection,nsurlrequest,Iphone,Ios,Xcode,Nsurlconnection,Nsurlrequest,我想知道如何仅获取返回值1或0。。。。异步从URL请求返回 目前我是这样做的: NSString *UTCString = [NSString stringWithFormat:@"http://web.blah.net/question/CheckQuestions?utc=%0.f",[lastUTCDate timeIntervalSince1970]]; NSLog(@"UTC String %@",UTCString); NSURL *updateDataURL = [NSURL UR

我想知道如何仅获取返回值1或0。。。。异步从URL请求返回

目前我是这样做的:

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得到回复,我如何设置它,使它在另一个线程而不是主线程中执行

编辑:回答 最后我用这个函数调用我的函数,它工作得很好:)


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

同步加载数据

+ sendSynchronousRequest:returningResponse:error:
+ connectionWithRequest:delegate:
– initWithRequest:delegate:
– initWithRequest:delegate:startImmediately:
– start
异步加载数据

+ sendSynchronousRequest:returningResponse:error:
+ connectionWithRequest:delegate:
– initWithRequest:delegate:
– initWithRequest:delegate:startImmediately:
– start

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

您可以使用
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 
您可以找到NSURLConnection的实现

编辑:尽管苹果公司提供了
NSURLConnection
,但更推荐的方式是放置URL请求。但我发现这个库非常节省时间,易于实现,而且健壮但与第三方实现一样简单。你应该试试看。

试试这个:

.h:

.m:


iOS XCode文档中有一个名为LazyTableImages的示例。这会在滚动停止后将异步URL和异步图像加载到屏幕上显示的
UITableView
单元格中。协议、异步数据处理等方面的优秀示例。

无耻地从中复制。所有学分都归我所有


这里是链接:我只需要一个1或0的回复,这是不是太多了?@Maulik添加“智能开发者”房间。从iOS 5开始,您可以使用
+(void)sendAsynchronousRequest:(NSURLRequest*)请求队列:(NSOperationQueue*)队列完成处理程序:(void(^)(NSURLResponse*,NSData*,NSError*))处理程序
,对于简单请求,这比创建委托对象更容易、更清晰。我稍后会尝试写一个显示此方法的答案。@MarkAmery很高兴看到您的答案。我很难找到
sendAsynchronousRequest
的简单示例。我应该使用哪种方法来获取cookie值??
- (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];
}
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];
                       }];