关于CocoaWebResource加载进度的方法

  由于需要实现把本地文件上传到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的消息机制的实现类。详细介绍可以看: http://www.cnblogs.com/xunziji/p/3257447.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%,达到了就发送上传完成的消息。

基本就是这样了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值