Here是我的实际问题,因为一些人建议我想编写一个类来处理UITableView中的多个下载进度。我不知道如何为此撰写课程,有人可以提供一些提示或想法吗?如何编写更新下载进度的iOs
0
A
回答
0
要查看的组是NSURLRequest和NSURLConnection。前者让你指定请求(URL,http方法,参数等),后者运行它。
由于您想更新状态(我想是想更新状态(I answered a sketch of this in your other question)),您需要实现NSURLConnectionDelegate协议,该协议在连接到达时移交数据块。如果你知道有多少数据预期,你可以用收到的金额来计算downloadProgress浮动正如我前面建议:
float downloadProgress = [responseData length]/bytesExpected;
下面是一些nice looking example code在SO。您可以延长这样的多个连接...
MyLoader.m
@interface MyLoader()
@property (strong, nonatomic) NSMutableDictionary *connections;
@end
@implementation MyLoader
@synthesize connections=_connections; // add a lazy initializer for this, not shown
// make it a singleton
+ (MyLoader *)sharedInstance {
@synchronized(self) {
if (!_sharedInstance) {
_sharedInstance = [[MyLoader alloc] init];
}
}
return _sharedInstance;
}
// you can add a friendlier one that builds the request given a URL, etc.
- (void)startConnectionWithRequest:(NSURLRequest *)request {
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSMutableData *responseData = [[NSMutableData alloc] init];
[self.connections setObject:responseData forKey:connection];
}
// now all the delegate methods can be of this form. just like the typical, except they begin with a lookup of the connection and it's associated state
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSMutableData *responseData = [self.connections objectForKey:connection];
[responseData appendData:data];
// to help you with the UI question you asked earlier, this is where
// you can announce that download progress is being made
NSNumber *bytesSoFar = [NSNumber numberWithInt:[responseData length]];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[connection URL], @"url", bytesSoFar, @"bytesSoFar", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyDownloaderDidRecieveData"
object:self userInfo:userInfo];
// the url should let you match this connection to the database object in
// your view controller. if not, you could pass that db object in when you
// start the connection, hang onto it (in the connections dictionary) and
// provide it in userInfo when you post progress
}
0
我写this库来这样做。您可以在github回购中签出实施。
相关问题
- 1. 了解更新UIprogressView的下载进度
- 2. 如何在Python中编写下载进度指示器?
- 3. 如何通过进度条更新实现多个下载?
- 4. 更新iOS 5中的进度条
- 5. iOS的进度条不更新
- 6. 如何更新进度栏?
- 7. 如何更新进度栏?
- 8. 下载进度
- 9. 如何给下载的进度条?
- 10. 如何计算ProgressBar的下载进度
- 11. 如何播放在IOS中下载进度视频文件
- 12. 如何编写下载的python脚本?
- 13. 如何编写一个进度条
- 14. 在下载时更新ListView中的进度栏
- 15. 更新从服务到活动的下载进度
- 16. 下载期间的Firemonkey OSX更新进度条
- 17. 进度条更新进度
- 18. 通过下载更新Plist文件iOS
- 19. Android下载进度
- 20. 下载进度条
- 21. SwiftyDropbox下载进度
- 22. NSURLConnection下载进度
- 23. 如何从FTP显示下载进度
- 24. 如何displaly进度条,下载附件
- 25. 如何更新jQuery UI的进度条
- 26. 如何显示subrepository更新的进度?
- 27. 更新进度
- 28. 更新进度
- 29. Activity与ProgressBar - >服务 - > AsyncTask下载 - 但如何更新进度?
- 30. 如何在下载过程中控制浏览器进度更新?
但在我的代码中,我在同一时间处理多个下载,我想单击一下按钮下载一组文件。所以我使用asinetworkqueue。所以我为队列设置了downloadp rogress委托。 – Mithuzz
这是多个相同的模式。对不起,我没有注意到你的问题的这一方面。将编辑。 – danh