如何在我的应用程序中显示UIProgressView进行下载?

时间:2010-02-05 04:35:05

标签: iphone nsurlconnection uiprogressview uiprogressbar

我想显示一个进度条,指示我的iPhone应用中已下载了多少文件。我知道如何在IB中设置UIProgressView以及所有这些。但我需要数据,如文件大小,以字节为单位来运行它。如何将此类功能与我的字节下载代码集成(如下所示)?

- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data
// A delegate method called by the NSURLConnection as data arrives.  We just 
// write the data to the file.
{
#pragma unused(theConnection)
    NSInteger       dataLength;
    const uint8_t * dataBytes;
    NSInteger       bytesWritten;
    NSInteger       bytesWrittenSoFar;

    assert(theConnection == self.connection);

    dataLength = [data length];
    dataBytes  = [data bytes];

    bytesWrittenSoFar = 0;
    do {
        bytesWritten = [self.fileStream write:&dataBytes[bytesWrittenSoFar] maxLength:dataLength - bytesWrittenSoFar];
        assert(bytesWritten != 0);
        if (bytesWritten == -1) {
            [self _stopReceiveWithStatus:@"File write error"];
            break;
        } else {
            bytesWrittenSoFar += bytesWritten;

        }
    } while (bytesWrittenSoFar != dataLength);
}

1 个答案:

答案 0 :(得分:2)

如果切换到使用ASIHTTPRequest库,则可以显示逐字节进度视图。

我真的建议你至少检查一下。它使这些东西在iPhone上非常简单,我将它用于我的所有应用程序。它将执行同步和异步连接,处理cookie和身份验证,并使POST组合请求变得非常简单。它还有一个内置的网络队列。

http://allseeing-i.com/ASIHTTPRequest/