简体   繁体   English

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

[英]How to show a UIProgressView for a download in my app?

I would like to show a progress bar indicating how much of a file has been downloaded in my iPhone app. 我想显示一个进度条,指示已在我的iPhone应用程序中下载了多少文件。 I know how to set up the UIProgressView in IB and all that. 我知道如何在IB中设置UIProgressView。 But I need data such as file size in bytes to run it. 但是我需要诸如字节大小的文件等数据来运行它。 How do I go about integrating such functionality with my byte download code (shown below)? 如何将此类功能与字节下载代码集成在一起(如下所示)?

- (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);
}

You can show show a byte-by-byte progress view if you switch to using the ASIHTTPRequest library. 如果切换到使用ASIHTTPRequest库,则可以显示逐字节进度视图。

I really recommend you at least check it out. 我真的建议您至少检查一下。 It makes this stuff really simple on the iPhone and I use it for all my apps. 它使这些东西在iPhone上非常简单,我将其用于所有应用程序。 It will do synchronous and asynchronous connections, deals with cookies and authentication, and makes composing POST requests really simple. 它将执行同步和异步连接,处理cookie和身份验证,并使编写POST请求变得非常简单。 It also has a built in network queue. 它还具有内置的网络队列。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM