IOS 下載文件Tools

使用方式:

? ? 第一個(gè)參數(shù)寫下載地址 不要拼接文件名稱

? ? 第二個(gè)參數(shù)寫存儲(chǔ)地址,有默認(rèn)地址可以不寫

? ? 第三個(gè)寫你下載的文件名

downloadTools * dtools = [downloadTools new];

[dtools downLoadRequest_downloadString:nil pathString:nil fileName:@"xxx.dwg"];

dtools.progressblock= ^(floatprogress) {

? ? ? ? nslog(@"下載中");

? ? ? ? ? ? ? ? };

? ??? dtools.finishedblock= ^{

? ? ? ? ? ? ? ? ? ? NSLog(@"下載完成");

? ? ? ? ? ? ? ? };

下面.h .m文件,可以直接粘貼

downloadTools.h

//

//? downloadTools.h

//? iOdaApp

//

//? Created by DSG on 2018/9/20.

//? Copyright ? 2018年 ODA. All rights reserved.

//

#import

//progress 數(shù)值為0~1

//下載中持續(xù)調(diào)用

typedefvoid(^progressBlock)(floatprogress);

//完成時(shí)調(diào)用

typedefvoid(^finishedBlock)(void);

@interfacedownloadTools :NSObject

/**


?*? 發(fā)起請求時(shí)調(diào)用(需要填入?yún)?shù) url路徑 (自動(dòng)轉(zhuǎn)換NSURL)、文件存儲(chǔ)路徑 (不要拼接文件名稱)、要下載文件的名稱(例如xxx.pdf或者xxxx.dwg))


?*? 如果 pathString 為 nil 則默認(rèn)路徑為[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject]


?*/

-(void)downLoadRequest_downloadString :(NSString*)downloadString pathString:(NSString*)pathString fileName:(NSString*)fileName;

/**


?*? 用來寫數(shù)據(jù)的文件句柄對象


?*/

@property (nonatomic,strong)NSFileHandle *writeHandle;

/**


?*? 文件的總大小


?*/

@property (nonatomic,assign)long long totalLength;

/**


?*? 當(dāng)前已經(jīng)寫入的文件大小


?*/

@property (nonatomic,assign)long long currentLength;

/**


?*? 下載過程中監(jiān)聽進(jìn)度


?*/

@property (nonatomic,strong) progressBlock progressblock;

/**


?*? 下載完成時(shí)調(diào)用


?*/

@property (nonatomic,strong) finishedBlock finishedblock;

@end


downloadTools.m

//

//? downloadTools.m

//? iOdaApp

//

//? Created by DSG on 2018/9/20.

//? Copyright ? 2018年 ODA. All rights reserved.

//

#import "downloadTools.h"

@implementation downloadTools

{

? ? NSString* receivePachString;


? ? NSString* receiveFileName;

}

#pragma mark - NSURLConnectionDataDelegate代理方法

/**


?*? 發(fā)起請求時(shí)調(diào)用(需要填入?yún)?shù) url路徑 (自動(dòng)轉(zhuǎn)換NSURL)、文件存儲(chǔ)路徑 (不要拼接文件名稱)、要下載文件的名稱(例如xxx.pdf或者xxxx.dwg))


?*/

-(void)downLoadRequest_downloadString :(NSString*)downloadString pathString:(NSString*)pathString fileName:(NSString*)fileName{


? ? receivePachString= pathString;


? ? receiveFileName= fileName;


? ? NSURL*url = [NSURLURLWithString:downloadString];


? ? NSURLRequest *request = [NSURLRequest requestWithURL:url];


? ? [NSURLConnection connectionWithRequest:request delegate:self];



}

/**


?*? 請求失敗時(shí)調(diào)用(請求超時(shí)、網(wǎng)絡(luò)異常)


?*


?*? @paramerror? ? ? 錯(cuò)誤原因


?*/

- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error

{


? ? NSLog(@"didFailWithError");


}

/**


?*? 1.接收到服務(wù)器的響應(yīng)就會(huì)調(diào)用 (先搞一個(gè)0kb的文件,然后用writeHandle關(guān)聯(lián)那個(gè)文件,最后寫入數(shù)據(jù)


?*? @paramresponse? 響應(yīng)


?*/

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response

{


? ? // 文件路徑 沙盒中的caches的路徑

? ? NSString*caches;


? ? if (receivePachString) {


? ? ? ? caches =receivePachString;


? ? }else{


? ? ? ? caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject];


? ? }


? ? NSLog(@"caches = %@",caches);



? ? //用這個(gè)stringByAppendingPathComponent:方法會(huì)自動(dòng)添加一個(gè)/表示這是個(gè)路徑


? ? NSString *filepath = [caches stringByAppendingPathComponent:receiveFileName];//先搞一個(gè)0kb的文件


? ? // 創(chuàng)建一個(gè)空的文件到沙盒中


? ? NSFileManager *mgr = [NSFileManager defaultManager];



? ? [mgrcreateFileAtPath:filepath contents:nil attributes:nil];



? ? // 創(chuàng)建一個(gè)用來寫數(shù)據(jù)的文件句柄


? ? self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filepath];


? ? // 獲得文件的總大小


? ? self.totalLength = response.expectedContentLength;


}

/**


?*? 2.當(dāng)接收到服務(wù)器返回的實(shí)體數(shù)據(jù)時(shí)調(diào)用(具體內(nèi)容,這個(gè)方法可能會(huì)被調(diào)用多次)


?*


?*? @paramdata? ? ? 這次返回的數(shù)據(jù)


?*/

- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data

{


? ? // 移動(dòng)到文件的最后面


? ? [self.writeHandle seekToEndOfFile];





? ? // 將數(shù)據(jù)寫入沙盒(先移動(dòng)到最后面再拼接)


? ? [self.writeHandlewriteData:data];//最后寫入數(shù)據(jù)




? ? // 累計(jì)文件的長度


? ? self.currentLength+= data.length;




? ? NSLog(@"下載進(jìn)度:%f", (double)self.currentLength/self.totalLength);


? ? //執(zhí)行progressBlock

? ? if (self.progressblock) {


? ? ? ? self.progressblock((double)self.currentLength/self.totalLength);

? ? }

}

/**


?*? 3.加載完畢后調(diào)用(服務(wù)器的數(shù)據(jù)已經(jīng)完全返回后)


?*/

- (void)connectionDidFinishLoading:(NSURLConnection*)connection

{


? ? self.currentLength =0;


? ? self.totalLength =0;




? ? // 關(guān)閉文件


? ? [self.writeHandle closeFile];


? ? self.writeHandle =nil;


? ? //執(zhí)行finishedblock

? ? if (self.finishedblock) {


? ? self.finishedblock();


? ? }

}

@end

隨便寫了一下,需要的帶走,我就希望做點(diǎn)好事兒匹配的時(shí)候王者榮耀隊(duì)友輕點(diǎn)舉報(bào)我

最后,我心里只有水水,我對阿貍忠心耿耿

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容