我試圖在NSURLConnection中使用MBProgressHUD。帶NSURLConnection的MBProgressHUD
在MBProgressHUD的示範項目的例子報道:
- (IBAction)showURL:(id)sender {
NSURL *URL = [NSURL URLWithString:@"https://github.com/matej/MBProgressHUD/zipball/master"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
[connection release];
HUD = [[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES] retain];
HUD.delegate = self;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
expectedLength = [response expectedContentLength];
currentLength = 0;
HUD.mode = MBProgressHUDModeDeterminate;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
currentLength += [data length];
HUD.progress = currentLength/(float)expectedLength;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] autorelease];
HUD.mode = MBProgressHUDModeCustomView;
[HUD hide:YES afterDelay:2];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[HUD hide:YES];
}
運行它時,HUD確定模式旋轉的罰款。
我試圖實現這一點,但這裏
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
currentLength += [data length];
HUD.progress = currentLength/(float)expectedLength;
}
圈是空的,沒有填充。
我不知道它是否取決於請求的網址的維度。
我請求讓我從我的網站下載的plist(〜80 KB),但圈內一直是空的,控制檯報告
<Error>: void CGPathAddArc(CGPath*, const CGAffineTransform*, CGFloat, CGFloat, CGFloat, CGFloat, CGFloat, bool): invalid value for start or end angle.
我甚至試圖做到這樣:
float progress = 0.0f;
while (progress < 1.0f) {
progress += 0.01f;
HUD.progress = progress;
}
但是現在這個圈子已經滿了,並沒有做任何動畫。
我認爲這取決於請求的網址的維度,但我不太確定,有誰知道如何解決這個問題嗎?
這確實可以解決問題,但爲什麼?你能解釋爲什麼這會導致服務器返回內容長度,否則它不會? –
沒有在我的情況下工作....雖然upvoted你的答案.... – TheMall
與'NSURLSession'相同的問題,它爲totalexpectedbytes返回'NSURLSessionTransferSizeUnknown'(-1)作爲響應下載。這種情況很可能發生在服務器返回「gzip」ed響應時,您的回答使我朝正確的方向發展。爲「Accept-Encoding」設置空白將告訴服務器客戶端未準備好接受任何特定格式(如「gzip」)的響應。所以服務器簡單地繞過「gzip」ping響應流並返回原始字節。幸運的是'NSURLSession'(和'NSURLconnection')可以得到預期的no。來自它的字節。 –