关于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%,达到了就发送上传完成的消息。
基本就是这样了。
它的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%,达到了就发送上传完成的消息。
基本就是这样了。
相关文章推荐
- 关于未能加载文件或程序集 system.web.extensions解决方法
- 关于WKWebView加载完毕的代理方法
- 关于Webpack dev server热加载失败的解决方法
- 在WebView中加载HTML页面时显示进度对话框的方法
- 关于安卓webview加载网页黑屏、部分黑屏或blank的解决方法
- 关于未能加载文件或程序集 system.web.extensions解决方法
- 关于未能加载文件或程序集 system.web.extensions解决方法
- 关于webview加载h5提示当前网站证书不可信的一些处理方法
- android 关于webview 加载h5网页开启定位的方法
- 关于未能加载文件或程序集 system.web.extensions解决方法
- BitmapFactory.decodeResource加载图片缩小的原因及解决方法
- Web项目:The requested resource is not available解决方法
- 关于使用MyEclipse启动web项目时工具报 The specified JRE installation does not exist 错误提示解决方法
- 关于上班第一天安装CocoaPod安装成功并使用的方法和感受
- 关于提示webresource.axd未注册的问题
- Android--当webview加载网页的时候在标题栏上显示加载进度
- 关于android webview 图片使用同一个src导致只加载第一张的问题
- LINUX驱动学习:加载模块时出现Device or resource busy的解决方法
- 加载web项目的路径的方法
- 关于Hibernate 在Junit测试中可以成功但是在Web下却无法运行的处理方法