如何使用ASIHTTPRequest(ASYNC)跟踪下载进度

max*_*ax_ 4 iphone objective-c uiprogressview asihttprequest

我目前正在我的网站上使用异步调用我的API(我设置).我正在使用ASIHTTPRequest的setDownloadProgressDelegate和UIProgressView.但是我不知道如何调用一个选择器(updateProgress),它将把一个CGFloat'progress'设置为progressView的进度.我尝试了以下,但两个进展都是零.请问你能告诉我如何才能使这个工作?

(in some method)

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[url stringByAppendingFormat:@"confidential"]]];
    [request setDownloadProgressDelegate:progressView];
    [NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES];
    [request setCompletionBlock:^{~100 lines of code}];
    [request setFailedBlock:^{~2 lines of code :) }];
    [request startAsynchronous];

- (void) updateProgress:(NSTimer *)timer {
    if (progressView.progress < 1.0) {
        currentProgress = progressView.progress;
        NSLog(@"currProg: %f --- progressViewProg: %f", currentProgress, progressView.progress);
    }
    else {
        [timer invalidate];
    }
    return;
}
Run Code Online (Sandbox Code Playgroud)

Jos*_*phH 7

对于仍然找到这个答案的人:请注意ASI被高度弃用,您应该使用NSURLSession或ASIHTTPRequest.

实现您想要的一种方法是将其设置downloadProgressDelegate为您自己的类并实现setProgress:.在此实现中,更新您的进度变量,然后调用[progressView setProgress:];

或者在代码中,设置请求的下载进度委托:

[request setDownloadProgressDelegate:self];
Run Code Online (Sandbox Code Playgroud)

然后将该方法添加到您的类:

- (void)setProgress:(float)progress
{
    currentProgress = progress;
    [progressView setProgress:progress];
}
Run Code Online (Sandbox Code Playgroud)