MBProgressHUD与NSURLConnection

Phi*_*lip 3 objective-c 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];
}
Run Code Online (Sandbox Code Playgroud)

运行它,确定模式下的HUD旋转正常.

我试图实现这个,但在这里

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        currentLength += [data length];
        HUD.progress = currentLength / (float)expectedLength;
    }
Run Code Online (Sandbox Code Playgroud)

圆圈是空的,没有填充.

我不知道它是否取决于所请求网址的维度.

我请求从我的网站下载一个plist(~80 kb),但是这个圆圈一直是空的并且控制台报告

<Error>: void CGPathAddArc(CGPath*, const CGAffineTransform*, CGFloat, CGFloat, CGFloat, CGFloat, CGFloat, bool): invalid value for start or end angle.
Run Code Online (Sandbox Code Playgroud)

我甚至试图这样做:

float progress = 0.0f;
    while (progress < 1.0f) {
        progress += 0.01f;
        HUD.progress = progress;
    }
Run Code Online (Sandbox Code Playgroud)

但现在圆圈已经完全没有做任何动画.

我认为这取决于所请求网址的维度,但我不太确定,有谁知道如何解决这个问题?

Phi*_*lip 7

我用这种方式解决了这个问题,切换NSURLRequestNSMutableURLRequest并将值none设置为编码(以前在gzip中)

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:anURL];
[request addValue:@"" forHTTPHeaderField:@"Accept-Encoding"];
Run Code Online (Sandbox Code Playgroud)

  • `NSURLSession` 有同样的问题,它返回 `NSURLSessionTransferSizeUnknown`(-1) for totalexpectedbytes 以响应下载。当服务器返回“gzip”响应并且您的回答使我朝着正确的方向前进时,最有可能发生这种情况。将“Accept-Encoding”设置为空白告诉服务器客户端还没有准备好接受任何特定格式(如“gzip”)的响应。所以服务器简单地绕过“gzip”ping 响应流并返回原始字节。幸运的是`NSURLSession`(和`NSURLconnection`)可以得到预期的数字。来自它的字节数。 (2认同)

Mar*_*n R 6

你应该检查[response expectedContentLength]in 的值didReceiveResponse.

http服务器可以省略"Content-Length"标头,而是使用"Transfer-Encoding:chunked".在这种情况下,内容长度不是先验已知的并且[response expectedContentLength]返回NSURLResponseUnknownLength(即-1).

我可以想象设置HUD.progress为负值会导致CGPathAddArc控制台消息.

根据文档,也可能发生累积currentLength变得大于预期的响应长度,因此您也应该检查它.