NSURLConnection 獲取要下載文件的大小信息

NSURLConnectionDataDelegate方法中有: app

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //This method is called when the server has determined that it 
    //has enough information to create the NSURLResponse.
    
    //It can be called multiple times, for example in the case of a 
    //redirect, so each time we reset the data.
    
    //receivedData is an instance variable declared elsewhere.
    
    NSLog(@"要下載文件大小爲 %lld",response.expectedContentLength);
    [self.receivedData setLength:0];
}

//在此方法中能夠更新進度條 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //provide an indication of the connection's progress to the user.
    //progressView.progress = [self.receivedData length] / totalSize

    [self.receivedData appendData:data];
}
相關文章
相關標籤/搜索