天天看点

iOS中 断点下载详解

版权声明:本文为博主原创文章,未经博主允许不得转载。

布局如下:

iOS中 断点下载详解

基本拖拉属性:

#import "viewcontroller.h"  

#import "afnetworking.h"  

@interface viewcontroller ()  

@property (weak, nonatomic) iboutlet uilabel *progresslabel;  

@property (weak, nonatomic) iboutlet uiprogressview *progressview;  

@property (nonatomic, strong) afhttprequestoperation *operation;  

@end  

@implementation viewcontroller  

调用:

- (void)viewdidload  

{  

    [super viewdidload];  

    nsstring *cachepath = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes).lastobject;  

    nsstring *txtpath = [cachepath stringbyappendingpathcomponent:@"mvtemp/mv.txt"];  

    nsfilemanager *filemanager = [nsfilemanager defaultmanager];  

    if ([filemanager fileexistsatpath:txtpath]) {  

        self.progressview.progress = [[nsstring stringwithcontentsoffile:txtpath encoding:nsutf8stringencoding error:nil] floatvalue];  

        self.progresslabel.text = [nsstring stringwithformat:@"%.2f%%", _progressview.progress * 100];  

    } else {  

        self.progressview.progress = 0;  

        self.progresslabel.text = @"0%";  

    }  

    nslog(@"%@", nshomedirectory());  

}  

点击事件:

- (ibaction)startorcanceldownload:(uibutton *)sender  

    if ([sender.currenttitle isequaltostring:@"开始下载"]) {  

        [sender settitle:@"暂停下载" forstate:uicontrolstatenormal];  

        nsstring *cachepath = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes).lastobject;  

        nsstring *filepath = [cachepath stringbyappendingpathcomponent:@"mv"];  

        nsstring *temppath = [cachepath stringbyappendingpathcomponent:@"mvtemp"];  

        nsfilemanager *filemanager = [nsfilemanager defaultmanager];  

        if (![filemanager fileexistsatpath:filepath]) {  

            [filemanager createdirectoryatpath:filepath withintermediatedirectories:yes attributes:nil error:nil];  

        }  

        if (![filemanager fileexistsatpath:temppath]) {  

            [filemanager createdirectoryatpath:temppath withintermediatedirectories:yes attributes:nil error:nil];  

        nsstring *mp4temppath = [temppath stringbyappendingpathcomponent:@"mv.temp"];  

        nsstring *txttemppath = [temppath stringbyappendingpathcomponent:@"mv.txt"];  

        nsstring *mp4path = [filepath stringbyappendingpathcomponent:@"mv.mp4"];  

        nsurl *url = [nsurl urlwithstring:@"http://video.szzhangchu.com/1442395443772_5176326090.mp4"];  

        nsurlrequest *request = [nsurlrequest requestwithurl:url];  

        unsigned long long downloadbytes = 0;  

        if ([filemanager fileexistsatpath:mp4temppath]) {  

            downloadbytes = [self filesizeatpath:mp4temppath];  

            nsstring *range = [nsstring stringwithformat:@"bytes=%llu-", downloadbytes];  

            nsmutableurlrequest *mutablerequest = [request mutablecopy];  

            [mutablerequest setvalue:range forhttpheaderfield:@"range"];  

            request = mutablerequest;  

        if (![filemanager fileexistsatpath:mp4path]) {  

            self.operation = [[afhttprequestoperation alloc] initwithrequest:request];  

            [self.operation setoutputstream:[nsoutputstream outputstreamtofileatpath:mp4temppath append:yes]];  

            __weak typeof(self) weakself = self;  

            [_operation setdownloadprogressblock:^(nsuinteger bytesread, long long totalbytesread, long long totalbytesexpectedtoread) {  

                weakself.progressview.progress = (float)(totalbytesread + downloadbytes) / (float)(totalbytesexpectedtoread + downloadbytes);  

                weakself.progresslabel.text = [nsstring stringwithformat:@"%.2f%%", weakself.progressview.progress * 100];  

                nsstring *progress = [nsstring stringwithformat:@"%.3f", weakself.progressview.progress];  

                [progress writetofile:txttemppath atomically:yes encoding:nsutf8stringencoding error:nil];  

            }];  

            [_operation setcompletionblockwithsuccess:^(afhttprequestoperation *operation, id responseobject) {  

                [filemanager moveitematpath:mp4temppath topath:mp4path error:nil];  

                [filemanager removeitematpath:txttemppath error:nil];  

            } failure:^(afhttprequestoperation *operation, nserror *error) {  

            [_operation start];  

        [sender settitle:@"开始下载" forstate:uicontrolstatenormal];  

        [self.operation cancel];  

        _operation = nil;  

- (unsigned long long)filesizeatpath:(nsstring *)path  

    unsigned long long filesize = 0;  

    if ([filemanager fileexistsatpath:path]) {  

        nserror *error = nil;  

        nsdictionary *dict = [filemanager attributesofitematpath:path error:&error];  

        if (dict && !error) {  

            filesize = [dict filesize];  

    return filesize;  

最终效果如下:

iOS中 断点下载详解

用到的第三方数据请求:afnetworking,大家应该都有,这里不做介绍

原文地址:http://blog.csdn.net/qq_31810357/article/details/49882769