AI智能
改变未来

IOS断点续传技术


断点续传

@interface ViewController ()

{
__weak IBOutlet UIProgressView *_progressView;

__weak IBOutlet UILabel *rateLab;NSURLConnection *myConnection;NSFileHandle *myFileHandle; //文件句柄, 操作文件(对文件进行读写 )//记录当前已经有多少数据了long long currentFileSize;//文件总大小long long totalFileSize;

}
@end

  • (void)viewDidLoad {
    [super viewDidLoad];

    //每次进入, 先判断文件是否存在,如果存在则获取已经下载的大小, 否则创建文件
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:[self getFilePath]]) {

    NSDictionary *attributesDict = [fileManager attributesOfItemAtPath:[self getFilePath] error:nil];currentFileSize = [attributesDict[NSFileSize] longLongValue];NSLog(@\"已经下载了:%lldKB\", currentFileSize/1024);

    }
    else {
    [fileManager createFileAtPath:[self getFilePath] contents:nil attributes:nil];
    }

    //获取保存在本地的文件总大小, 更新进度值
    NSNumber *totalSize = [[NSUserDefaults standardUserDefaults] objectForKey:@“totalFileSize”];
    if (totalSize) {
    totalFileSize = [totalSize longLongValue];
    _progressView.progress = (currentFileSize)*1.0 / totalFileSize;
    rateLab.text = [NSString stringWithFormat:@\”%.02f%%\”, (currentFileSize)*1.0 / totalFileSize * 100];
    }

}

#pragma mark – 开始下载

  • (IBAction)start:(id)sender {

    NSString *urlStr = DownloadURL;

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];

    //根据文件已下载的大小,设置下载范围,从后面开始开始下载
    NSString *range = [NSString stringWithFormat:@“bytes=%lld-”, currentFileSize];
    [request setValue:range forHTTPHeaderField:@“RANGE”];

    myConnection = [NSURLConnection connectionWithRequest:request delegate:self];

}

#pragma mark – NSURLConnectionDataDelegate

  • (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
    NSLog(@“接收到服务器响应”);

    //计算文件总大小
    totalFileSize = response.expectedContentLength + currentFileSize;

    //将文件总大小保存在本地,下次进入App可以直接获取
    [[NSUserDefaults standardUserDefaults] setObject:@(totalFileSize) forKey:@“totalFileSize”];
    NSLog(@“文件总大小:%lldM”, totalFileSize/1024/1024);

    myFileHandle = [NSFileHandle fileHandleForUpdatingAtPath:[self getFilePath]];

    //将文件的指针移动到最后
    [myFileHandle seekToEndOfFile];
    }

  • (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
    NSLog(@“接收到数据”);
    currentFileSize = currentFileSize+data.length;

    _progressView.progress = (currentFileSize)*1.0 / totalFileSize;
    rateLab.text = [NSString stringWithFormat:@\”%.02f%%\”, (currentFileSize)*1.0 / totalFileSize * 100];

    //将data添加到文件的后面
    [myFileHandle writeData:data];
    }

  • (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
    NSLog(@“接收完成!”);

    [myFileHandle closeFile];

}

  • (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
    [myFileHandle closeFile];

    NSLog(@\”%@\”, error);

}

#pragma mark – 暂停(取消下载)

  • (IBAction)stop:(id)sender {

    [myConnection cancel];
    [myFileHandle closeFile];
    }

#pragma mark – 文件路径
-(NSString *)getFilePath
{
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@“Documents”];

filePath = [filePath stringByAppendingPathComponent:@\"qq.dmg\"];NSLog(@\"filePath:%@\", filePath);return filePath;

}

  • 点赞
  • 收藏
  • 分享
  • 文章举报

spurs_ping发布了16 篇原创文章 · 获赞 0 · 访问量 607私信关注

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » IOS断点续传技术