IOS 下载文件Tools

使用方式:

    第一个参数写下载地址 不要拼接文件名称

    第二个参数写存储地址,有默认地址可以不写

    第三个写你下载的文件名

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 数值为0~1

//下载中持续调用

typedefvoid(^progressBlock)(floatprogress);

//完成时调用

typedefvoid(^finishedBlock)(void);

@interfacedownloadTools :NSObject

/**


 *  发起请求时调用(需要填入参数 url路径 (自动转换NSURL)、文件存储路径 (不要拼接文件名称)、要下载文件的名称(例如xxx.pdf或者xxxx.dwg))


 *  如果 pathString 为 nil 则默认路径为[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject]


 */

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

/**


 *  用来写数据的文件句柄对象


 */

@property (nonatomic,strong)NSFileHandle *writeHandle;

/**


 *  文件的总大小


 */

@property (nonatomic,assign)long long totalLength;

/**


 *  当前已经写入的文件大小


 */

@property (nonatomic,assign)long long currentLength;

/**


 *  下载过程中监听进度


 */

@property (nonatomic,strong) progressBlock progressblock;

/**


 *  下载完成时调用


 */

@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代理方法

/**


 *  发起请求时调用(需要填入参数 url路径 (自动转换NSURL)、文件存储路径 (不要拼接文件名称)、要下载文件的名称(例如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];



}

/**


 *  请求失败时调用(请求超时、网络异常)


 *


 *  @paramerror      错误原因


 */

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

{


    NSLog(@"didFailWithError");


}

/**


 *  1.接收到服务器的响应就会调用 (先搞一个0kb的文件,然后用writeHandle关联那个文件,最后写入数据


 *  @paramresponse  响应


 */

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

{


    // 文件路径 沙盒中的caches的路径

    NSString*caches;


    if (receivePachString) {


        caches =receivePachString;


    }else{


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


    }


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



    //用这个stringByAppendingPathComponent:方法会自动添加一个/表示这是个路径


    NSString *filepath = [caches stringByAppendingPathComponent:receiveFileName];//先搞一个0kb的文件


    // 创建一个空的文件到沙盒中


    NSFileManager *mgr = [NSFileManager defaultManager];



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



    // 创建一个用来写数据的文件句柄


    self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filepath];


    // 获得文件的总大小


    self.totalLength = response.expectedContentLength;


}

/**


 *  2.当接收到服务器返回的实体数据时调用(具体内容,这个方法可能会被调用多次)


 *


 *  @paramdata      这次返回的数据


 */

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

{


    // 移动到文件的最后面


    [self.writeHandle seekToEndOfFile];





    // 将数据写入沙盒(先移动到最后面再拼接)


    [self.writeHandlewriteData:data];//最后写入数据




    // 累计文件的长度


    self.currentLength+= data.length;




    NSLog(@"下载进度:%f", (double)self.currentLength/self.totalLength);


    //执行progressBlock

    if (self.progressblock) {


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

    }

}

/**


 *  3.加载完毕后调用(服务器的数据已经完全返回后)


 */

- (void)connectionDidFinishLoading:(NSURLConnection*)connection

{


    self.currentLength =0;


    self.totalLength =0;




    // 关闭文件


    [self.writeHandle closeFile];


    self.writeHandle =nil;


    //执行finishedblock

    if (self.finishedblock) {


    self.finishedblock();


    }

}

@end

随便写了一下,需要的带走,我就希望做点好事儿匹配的时候王者荣耀队友轻点举报我

最后,我心里只有水水,我对阿狸忠心耿耿

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容