iOS AFN进行断点下载

//
//  ViewController.m
//  Download
//
//  Created by Congwang on 14/6/29.
//  Copyright (c) 2014   Congwang. All rights reserved.
//

#import  "ViewController.h"
#import
  "AFNetworking.h"
@interface  ViewController  ()
@property  ( retain ,  nonatomic )  IBOutlet  UIProgressView  *sliderView;
@property  ( retain ,  nonatomic )  IBOutlet  UILabel  *progress;
@property  ( nonatomic ,  retain )  AFHTTPRequestOperation   * operation;
@end

@implementation  ViewController

- (
void )viewDidLoad {
    [
super  viewDidLoad ];
   
  NSString  * txtTempPath =[  NSTemporaryDirectory () stringByAppendingPathComponent : @"mvTemp/mv.txt" ];
 
NSFileManager   fileManager =[ NSFileManager  defaultManager ];
   
  if  ([fileManager  fileExistsAtPath :txtTempPath])
    {
       
  _sliderView . progress   [[ NSString stringWithContentsOfFile :txtTempPath encoding : NSUTF8StringEncoding  error : nil ]  floatValue ];
    }
   
   
  else  _sliderView . progress  =  0 ;
   
  _progress . text  = [ NSString stringWithFormat : @"%.2f%%" , _sliderView . progress  * 100 ];
   
  // Do any additional setup after loading the view from its nib.
   
   
  NSLog ( @"%@" ,  NSHomeDirectory ());
}

- (
void )didReceiveMemoryWarning {
    [
super  didReceiveMemoryWarning ];
   
  // Dispose of any resources that can be recreated.
}



- (
IBAction )start:( UIButton  *)sender
{
  
  if ([sender. currentTitle  isEqualToString : @" 开始下载 " ])
      {
          [sender
  setTitle : @" 暂停下载 " forState : UIControlStateNormal ];
   
   
  NSURL  * url = [ NSURL URLWithString : @"http://www.demaxiya.com/app/index.php?m=play&vid=29996&quality=1" ];
   
  NSString  * CachePath =[ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory ,  NSUserDomainMask , YES ) firstObject ];
   
  NSFileManager   fileManager =[ NSFileManager  defaultManager ];
  
  NSLog ( @"%@" ,CachePath);
   
  NSString  * folderPath = [CachePath  stringByAppendingPathComponent : @"mv" ];
   
  NSString  * tempPath =[  NSTemporaryDirectory () stringByAppendingPathComponent : @"mvTemp" ];
 
  // 判断缓存文件夹和视频存放文件夹是否存在 , 如果不存在 , 就创建一个文件夹
   
  if  (![fileManager  fileExistsAtPath :folderPath])
    {
        [fileManager
  createDirectoryAtPath :folderPath withIntermediateDirectories : YES  attributes : nil  error : nil ];
    }
//
   
  if  (![fileManager  fileExistsAtPath :tempPath])
    {
        [fileManager
  createDirectoryAtPath :tempPath withIntermediateDirectories : YES  attributes : nil  error : nil ];
    }

   
  NSString  * tempFilePath = [tempPath  stringByAppendingPathComponent : @"mv.temp" ]; // 缓存路径
   
  NSString  * mvFilePath = [folderPath  stringByAppendingPathComponent : @"mv.mp4" ]; // 文件保存路径
   
  NSString  * txtFilePath = [tempPath  stringByAppendingPathComponent : @"mv.txt" ]; // 保存重启程序下载的进度
 
      
  unsigned  long  long   downloadedBytes = 0 ;
   
  NSURLRequest  * request = [ NSURLRequest    requestWithURL :url];
   
   
  if  ([fileManager  fileExistsAtPath :tempFilePath]) // 如果存在 , 说明有缓存文件
    {
        downloadedBytes = [
self  fileSizeAtPath :tempFilePath]; // 计算缓存文件的大小
       
  NSMutableURLRequest  *mutableURLRequest = [request mutableCopy ];
       
  NSString  *requestRange = [ NSString stringWithFormat : @"bytes=%llu-" , downloadedBytes];
       
        [mutableURLRequest
  setValue :requestRange forHTTPHeaderField : @"Range" ];
        request = mutableURLRequest;
       
  NSLog ( @"============== 断点下载 " );
    }

   
   
  if  (![fileManager   fileExistsAtPath :mvFilePath]) {
       
        [[
NSURLCache  sharedURLCache ]  removeCachedResponseForRequest :request];
      
  self . operation = [[ AFHTTPRequestOperation  alloc ] initWithRequest :request];
        [
_operation  setOutputStream :[ NSOutputStream outputStreamToFileAtPath : tempFilePath  append : YES ]];
     
        [
_operation    setDownloadProgressBlock :^( NSUInteger bytesRead,  long  long  totalBytesRead,  long  long totalBytesExpectedToRead) {
           
  _sliderView . progress  = (( float )totalBytesRead + downloadedBytes) / (totalBytesExpectedToRead + downloadedBytes);
           
  _progress . text  = [ NSString stringWithFormat : @"%.2f%%" , _sliderView . progress  * 100 ];
         
           
  NSString  * progress = [ NSString stringWithFormat : @"%.3f" ,(( float )totalBytesRead + downloadedBytes) / (totalBytesExpectedToRead + downloadedBytes)];
            [progress
  writeToFile :txtFilePath  atomically : YES encoding : NSUTF8StringEncoding  error : nil ];
        }];
       
        [
_operation    setCompletionBlockWithSuccess :^( AFHTTPRequestOperation  *operation,  id  responseObject)
         {
            
             [fileManager
  moveItemAtPath :tempFilePath toPath :mvFilePath  error : nil ]; // 把下载完成的文件转移到保存的路径
              [fileManager
  removeItemAtPath :txtFilePath error : nil ]; // 删除保存进度的 txt 文档
            
            
         }
  failure :^( AFHTTPRequestOperation  *operation,  NSError *error)
        
         {
            
            
         }];
        [
_operation  start ];
    }
else
    {
       
    }
      }
     
  else
      {
         
          [sender
  setTitle : @" 开始下载 " forState : UIControlStateNormal ];
          [
self . operation  cancel ];
          
  self . operation  =  nil ;
         
      }
   
}
// 计算缓存文件大小的方法
- (
unsigned  long  long )fileSizeAtPath:( NSString  *)fileAbsolutePath {
   
  signed  long  long  fileSize =  0 ;
   
  NSFileManager  *fileManager = [ NSFileManager  new ];
   
  if  ([fileManager  fileExistsAtPath :fileAbsolutePath]) {
       
  NSError  *error =  nil ;
       
  NSDictionary  *fileDict = [fileManager attributesOfItemAtPath :fileAbsolutePath  error :&error];
       
  if  (!error && fileDict) {
            fileSize = [fileDict
  fileSize ];
        }
    }
   
  return  fileSize;
}

@end
iOS <wbr>AFN进行断点下载
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值