如何在我的应用程序中显示用于下载的 UIProgressView?
我想显示一个进度条,指示我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您切换到使用 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/