【问题标题】:How to show a UIProgressView for a download in my app?如何在我的应用程序中显示 UIProgressView 以供下载?
【发布时间】:2010-02-05 04:35:04
【问题描述】:

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

【问题讨论】:

    标签: iphone nsurlconnection uiprogressview uiprogressbar


    【解决方案1】:

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

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

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

    【讨论】:

    • Naaaah...我的东西大部分已经设置好了..不想再进去开始改变东西了。不过谢谢..有什么方法可以获取服务器上文件的大小(以字节为单位)?这样我就可以使用 UIProgressView
    猜你喜欢
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 2020-06-07
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多