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

发布于 2024-08-20 12:19:29 字数 1059 浏览 20 评论 0原文

我想显示一个进度条,指示我的 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);
}

I would like to show a progress bar indicating how much of a file has been downloaded in my iPhone app. I know how to set up the UIProgressView in IB and all that. 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贩梦商人 2024-08-27 12:19:29

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

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

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

You can show show a byte-by-byte progress view if you switch to using the ASIHTTPRequest library.

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. It will do synchronous and asynchronous connections, deals with cookies and authentication, and makes composing POST requests really simple. It also has a built in network queue.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文