2010-02-15 88 views

回答

63

在你的NSURLConnection委託中,實現類似這樣的事情來找出總的內容長度。服務器必須支持這一點,但它很可能會很好地工作靜態內容:

- (void)connection: (NSURLConnection*) connection didReceiveResponse: (NSHTTPURLResponse*) response 
{ 
    statusCode_ = [response statusCode]; 
    if (statusCode_ == 200) { 
     download_.size = [response expectedContentLength]; 
    } 
} 

,然後更新這樣的進步:

- (void) connection: (NSURLConnection*) connection didReceiveData: (NSData*) data 
{ 
    [data_ appendData: data]; 
    download_.progress = ((float) [data_ length]/(float) download_.size); 
    // Broadcast a notification with the progress change, or call a delegate 
} 

在我來說,我有一個具有size下載實例和progress屬性。它們由全球DownloadManager對象擁有,該對象將負責通知有關各方下載進度或狀態更改。