您将不得不引入更多的代码来包含下载状态指示器栏。在下载数据的时候
[NSData dataWithConentsOfURL:...]
. 相反,您将创建一个使用
NSURLConnection
对象以下载数据,将该数据累积到MSMutableData对象中,并相应地更新用户界面。你应该能够使用
ContentLength
HTTP头和
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
更新以确定下载的状态。
以下是一些相关的方法:
- (void) startDownload
{
downloadedData = [[NSMutableData alloc] init];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
- (void)connection:(NSURLConnection *)c didReceiveResponse:(NSURLResponse *)response
{
totalBytes = [response expectedContentLength];
}
// assume you have an NSMutableData instance variable named downloadedData
- (void)connection:(NSURLConnection *)c didReceiveData:(NSData *)data
{
[downloadedData appendData: data];
float proportionSoFar = (float)[downloadedData length] / (float)totalBytes;
// update UI with proportionSoFar
}
- (void)connection:(NSURLConnection *)c didFailWithError:(NSError *)error
{
[connection release];
connection = nil;
// handle failure
}
- (void)connectionDidFinishLoading:(NSURLConnection *)c
{
[connection release];
connection = nil;
// handle data upon success
}
我个人认为,最简单的方法是创建一个实现上述方法的类来进行通用数据下载并与该类接口。
这应该足以满足你的需要。