分享

iOS wifi上传文件

 wintelsui 2017-12-29

利用Wi-Fi从pc端上传文件到iOS设备上

首先,从Github下载cocoa-web-resource

114019_fsTW_584517.png

pc浏览器运行的效果:

114343_Wubt_584517.png

代码中如果不想端口为大家所熟知的,可以随机生产一个端口号,在代码的操作很简单,只要在CocoaWebResourceViewController.m文件中注释[httpServer setPort:8080];这一行代码,以后开启server就是一个随机的端口号。

    cocoa-web-resource能进行上传各种文件,美中不足的是当上传一个大一点的文件,在pc的浏览器给人的感觉就是卡住了,体验不是很好,后来参考了博文:关于CocoaWebResource加载进度的方法。但博文讲的不是很详细,对于初学者来说,还是不知道怎么把进度条加上。在此博文的基础上,先声明一个全局变量progressView,初始化:

?

1
2
3
    progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(30, 150, 250, 10)];
    progressView.progress = 0;
    [self.view addSubview:progressView];


    三个监听的事件:

?

1
2
3
    [[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];


    三个监听的函数:

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma mark 监听上传的过程
//开始上传
-(void)uploadingStart:(NSNotification *)notification
{
    NSLog(@"start");
}
//正在上传
-(void)uploadingProgress:(NSNotification *)notification
{
    NSString *progress = notification.object;
    progressView.progress = [progress floatValue];
    NSLog(@"progress = %@",progress);
}
//上传完成
-(void)uploadingFinish:(NSNotification *)notification
{
    NSLog(@"finish");
}


这一来,上传文件的进度条就有了。

    该demo还有一个好处,如果你想要显示上传在设备上的文件,你可以用uitableview通过此数组fileList来展现,然后你在此函数- (void)loadFileList最后加上[listTable reloadData];每次上传完或delete之后,数据会自动刷新,基本上满足的需求。


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多