目前封裝最好使用最簡單的文件下載第三方iOS網絡下載庫

jopen 9年前發布 | 60K 次閱讀 IOS iOS開發 移動開發

/*

  • qq:712641411
  • iOS大神qq群:460122071 */

目前封裝最好使用最簡單的文件下載第三方iOS網絡下載庫

具體使用方式請下載demo閱讀里面很詳細

運行效果

image

Use Example

//開始下載文件代碼片段
BOOL            _isDownload;
//保存下載文件路徑
NSString      * _filePath = [NSString stringWithFormat:@"%@/Library/Caches/WHCFiles/",NSHomeDirectory()]; 
//自定義存儲下載文件名
NSString      * _fileName = @"吳海超下載測試文件.mp4";
//當前下載對象
WHC_Download  * _download = [WHCDownloadCenter startDownloadWithURL:url 
                                                           savePath:_filePath  
                                                       savefileName:_fileName 
                                                           delegate:self];

//下載代理實現
#pragma mark - WHCDownloadDelegate
//得到第一響應
- (void)WHCDownload:(WHC_Download *)download didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"下載開始");
}

//接受下載數據處理下載顯示進度以及下載速度
- (void)WHCDownload:(WHC_Download *)download didReceivedLen:(uint64_t)receivedLen totalLen:(uint64_t)totalLen networkSpeed:(NSString *)networkSpeed{
    CGFloat  percent = (CGFloat)receivedLen / totalLen * 100.0;
    _percentLab.text = [NSString stringWithFormat:@"%.1f%%",percent];  //顯示下載百分比
    _downProgressV.progress = percent / 100.0;                         //顯示下載進度
    //顯示下載文件大小
    _curDownloadSizeLab.text = [NSString stringWithFormat:@"%.1fMB/%.1fMB",((CGFloat)receivedLen / 1024.0) / 1024.0 ,((CGFloat)totalLen / 1024.0) / 1024.0];
    _downloadSpeedLab.text = networkSpeed; //顯示當前下載速度
}

//下載出錯處理
- (void)WHCDownload:(WHC_Download *)download error:(NSError *)error{
    if(error){
        NSString  * strError = error.description;
        switch (error.code) {
            case GeneralErrorInfo:
                NSLog(@"一般出錯");
            break;
            case NetWorkErrorInfo:
                NSLog(@"網絡錯誤");
            break;
            case FreeDiskSpaceLack:
                NSLog(@"磁盤剩余空間不足");
            break;
            default:
            break;
        }
        UIAlertView  * alert = [[UIAlertView alloc]initWithTitle:@"下載出錯誤" message:strError delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
        [alert show];
    }
    _isDownload = NO;
}

//下載結束處理
- (void)WHCDownload:(WHC_Download *)download filePath:(NSString *)filePath isSuccess:(BOOL)success{
    NSLog(@"filePath = %@",filePath);
    if(success){
        UIAlertView  * alert = [[UIAlertView alloc]initWithTitle:@"阿超已經幫你下載完成" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
        [alert show];
    }
    _isDownload = NO;
}

項目主頁:http://www.baiduhome.net/lib/view/home/1438147065035

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!