2011-12-15 93 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]; 
         }];