溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

iOS wifi上傳文件

發(fā)布時(shí)間:2020-06-29 15:53:11 來源:網(wǎng)絡(luò) 閱讀:578 作者:wangjinhan89 欄目:移動(dòng)開發(fā)

利用Wi-Fi從pc端上傳文件到iOS設(shè)備上

首先,從Github下載cocoa-web-resource:

iOS wifi上傳文件

pc瀏覽器運(yùn)行的效果:

iOS wifi上傳文件

代碼中如果不想端口為大家所熟知的,可以隨機(jī)生產(chǎn)一個(gè)端口號(hào),在代碼的操作很簡(jiǎn)單,只要在CocoaWebResourceViewController.m文件中注釋[httpServer setPort:8080];這一行代碼,以后開啟server就是一個(gè)隨機(jī)的端口號(hào)。

    cocoa-web-resource能進(jìn)行上傳各種文件,美中不足的是當(dāng)上傳一個(gè)大一點(diǎn)的文件,在pc的瀏覽器給人的感覺就是卡住了,體驗(yàn)不是很好,后來參考了博文:關(guān)于CocoaWebResource加載進(jìn)度的方法。但博文講的不是很詳細(xì),對(duì)于初學(xué)者來說,還是不知道怎么把進(jìn)度條加上。在此博文的基礎(chǔ)上,先聲明一個(gè)全局變量progressView,初始化:

?

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


    三個(gè)監(jiān)聽的事件:

?

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];


    三個(gè)監(jiān)聽的函數(shù):

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma mark 監(jiān)聽上傳的過程
//開始上傳
-(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");
}


這一來,上傳文件的進(jìn)度條就有了。

    該demo還有一個(gè)好處,如果你想要顯示上傳在設(shè)備上的文件,你可以用uitableview通過此數(shù)組fileList來展現(xiàn),然后你在此函數(shù)- (void)loadFileList最后加上[listTable reloadData];每次上傳完或delete之后,數(shù)據(jù)會(huì)自動(dòng)刷新,基本上滿足的需求。


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI