您的位置:首页 > 其它

关于CocoaWebResource加载进度的方法

2014-03-25 17:55 309 查看
由于需要实现把本地文件上传到IOS设备的功能,在网上搜了一下,发现了CocoaWebResource这个开源项目,https://github.com/robin/cocoa-web-resource

它的demo就是一个简单的文件服务器了。

不过这个demo里没有实现上传进度的显示。我就浏览了一下源代码,发现在HTTPServer.h的头部定义了

#define HTTPUploadingStartNotification @"UploadingStarted"

#define HTTPUploadingProgressNotification @"UploadingProgress"

#define HTTPUploadingFinishedNotification @"UploadingFinished"

#define HTTPFileDeletedNotification @"FileDeleted"
然后在HTTPConnection.m的handleMultipartHeader函数中调用了

[[NSNotificationCenter
defaultCenter] postNotificationName:HTTPUploadingStartNotification
object:filename];

在HTTPConnection.m 的handleHTTPRequestBody中调用了

[[NSNotificationCenter
defaultCenter] postNotificationName:HTTPUploadingProgressNotification
object:progress];

NSNotificationCenter 是IOS的消息机制的实现类。详细介绍可以看:/article/5084617.html

HTTPUploadingStartNotification就是通知开始上传,传递文件名

HTTPUploadingProgressNotification
就是通知正在上传,传递进度值

看了这些代码和NSNotificationCenter的使用后,基本就知道怎么实现进度的显示了。
我在该demo的CocoaWebResourceViewControler.m的viewDidLoad中添加的部分代码

[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(uploadingStart:)
name:@"UploadingStarted"
object:nil];

[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(uploadingProgress:)
name:@"UploadingProgress"
object:nil];

[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(uploadingFinish:)
name:@"UploadingFinished"
object:nil];
然后实现uploadingStart, uploadingProgress,uploadingFinish的功能

//开始上传

-(void)uploadingStart:(NSNotification *)notification

{

}

//正在上传

-(void)uploadingProgress:(NSNotification *)notification

{

}

//上传完成

-(void)uploadingFinish:(NSNotification *)notification

{

}
不过没发现HTTPConnection.m中有添加HTTPUploadingFinishedNotification上传完成的进度消息提示
于是我就在HTTPContenction.m的handleHTTPRequestBody最后添加了一部分代码

if ([progress
floatValue] == 1.0) {

[[NSNotificationCenter
defaultCenter] postNotificationName:HTTPUploadingFinishedNotification
object:nil];
}

用于判断进度值是不是达到了100%,达到了就发送上传完成的消息。

基本就是这样了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: