2011-12-15 97 views
21

我想知道如何获得仅返回值1或0 ....异步URL请求。如何发送异步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回来了,我该如何设置它,所以它会做它另一个线程而不是主线程?

编辑:ANSWER 我结束我的upcalling功能与此,它工作得很好:)

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

作为iOS 5的,则可以使用'+(无效)sendAsynchronousRequest:(的NSURLRequest *)请求队列:(NSOperationQueue *)队列completionHandler:(无效(^)(NSURLResponse *,NSData的*,NSError *) )handler`,这比创建一个委托对象更简单,更简单。我会尽量写出一个答案,稍后再显示这种方法。 – 2013-07-31 15:30:35

+1

@MarkAmery很高兴看到您的答案。我很难找到一个简单的例子`sendAsynchronousRequest`。 – 2014-06-22 23:44:54

回答

28

可以使用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请求的更多推荐的方式。但是,我发现AFNetworking库非常省时,易于实现,并且强大而简单,因为它是第三方实现的。你应该试试看。

+1

我应该使用哪种方法来获取cookie值 – kAnNaN 2013-10-30 10:51:41

5

使用NSURLConnection,让你的要求。 然后您就可以开始与NSURLConnection的的方法同步或异步连接:

加载数据同步方式

+ sendSynchronousRequest:returningResponse:error: 

加载数据不同步

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

检查苹果开发者API参考的NSURLConnection类。

+0

这里是链接:http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html – samfisher 2011-12-15 05:55:42

26

试试这个:

.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]; 
} 
2

有一个在iOS版的XCode文档称为LazyTableImages一个例子。在滚动停止后,这会在屏幕上显示一个异步URL以及异步图像加载到UITableView单元格。协议的优秀示例,异步数据处理等。

3

无耻地从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]; 
         }];