2011-08-10 128 views
0

我試圖使用NSURLRequestNSURLConnection從Web API獲取一小部分數據(大約50字節)。我很確定下面的代碼是正確的,但是我怎樣才能真正獲取收到的數據並檢測其全部下載的時間?如何檢測NSURLConnection何時完成

NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0]; 
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:req delegate:self]; 

回答

5

通過實施NSURLConnectionDelegate,您可以在connection:didReceiveData:方法獲取數據和connectionDidFinishLoading:方法將被調用時的操作結束。同時,如果在操作過程中出現錯誤,將會調用connection:didFailWithError:

+0

Documentation page [here](http://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html) – bobobobo