iOS 利用 ASI 实现的异步队列下载文件
来源:互联网 发布:c4d中文版软件 编辑:程序博客网 时间:2024/05/19 03:23
概述: 首先 导入ASI网络请求框架 ASI 需要导入的系统库 : CFNetwork.framework SystemConfiguration.framework MobileCoreServices.framework CoreGraphics.framework libz.dylib
本功能主要使用到 ASIHTTPRequest 和 ASINetworkQueue
.h 文件
#import <Foundation/Foundation.h>
#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
@interface DownLoadManage :NSObject
@property (nonatomic)double fileSiz;//文件全部大小
@property (nonatomic)longlong DidDownLoadLenth; //已经下载的文件大小
@property (nonatomic,strong)ASINetworkQueue *netWorkQueue; //下载队列
+ (DownLoadManage *)DefaultManage;
-(void)createQuue;
- (void)startDownLoadFileByFile:(SongsList *)songlist;
#import "DownLoadManage.h"
#define RequestOutTime 120 //设置超时时间
@implementation DownLoadManage
staticDownLoadManage *downLoadManage = nil;
+ (id)DefaultManage{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
downLoadManage = [[DownLoadManagealloc] init];
});
returndownLoadManage;
}
//创建异步队列
- (void)createQuue {
if (self.netWorkQueue ==nil) {
ASINetworkQueue *que = [[ASINetworkQueuealloc] init];
self.netWorkQueue = que;
[self.netWorkQueuereset];
[self.netWorkQueuesetShowAccurateProgress:YES];
[self.netWorkQueuesetShouldCancelAllRequestsOnFailure:NO];
[self.netWorkQueuesetMaxConcurrentOperationCount:1];
[self.netWorkQueuego];
// 创建存放路径
//初始化Documents路径
NSString *path = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];
//初始化临时文件路径
NSString *folderPath = [path stringByAppendingPathComponent:@"/DownLoad/temp"];
//创建文件管理器
NSFileManager *fileManager = [NSFileManagerdefaultManager];
//判断temp文件夹是否存在
BOOL fileExists = [fileManager fileExistsAtPath:folderPath];
if (!fileExists) {//如果不存在说创建,因为下载时,不会自动创建文件夹
[fileManagercreateDirectoryAtPath:folderPath
withIntermediateDirectories:YES
attributes:nil
error:nil];
}
}
}
//创建请求并将请求加入队列
- (void)startDownLoadFileByFile:(NSString *)fileName {
//初始化保存路径
NSString *savePath = [FileSavePath stringByAppendingPathComponent:[NSStringstringWithFormat:@"DownLoad/%@.mp3",fileName]];
//初始下载连接
NSString *strUrl = [NSStringstringWithFormat:@"http://bcs.duapp.com/preschoolteaching/song/%@.mp3",songlist.songName];
NSString *URL = [strUrlstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURLURLWithString:URL];
//设置下载连接
ASIHTTPRequest *request = [[ASIHTTPRequestalloc] initWithURL:url];
//设置ASIHTTPRequest代理
request.delegate =self;
//初始化保存ZIP文件路径
NSString *savePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"book_%d.zip",[sender tag]]];
//初始化临时文件路径
NSString *tmpDir =NSTemporaryDirectory();
NSString *tempPath = [tmpDirstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.mp3.temp",songlist.songName]];
//设置文件保存路径
[request setDownloadDestinationPath:savePath];
//设置临时文件路径
[request setTemporaryFileDownloadPath:tempPath];
//设置进度条的代理,
[request setDownloadProgressDelegate:self];
//设置是是否支持断点下载
[request setAllowResumeForFileDownloads:YES];
//设置超时时间
[request setTimeOutSeconds:RequestOutTime];
//设置基本信息 用来标记请求
[requestsetUserInfo:[NSDictionarydictionaryWithObjectsAndKeys:songlist.songTag,@"bookID",songlist.songName,@"fileName",nil]];
//添加到ASINetworkQueue队列去下载
[self.netWorkQueueaddOperation:request];
}
#pragma mark ASIHTTPRequestDelegate method
//ASIHTTPRequestDelegate,下载之前获取信息的方法,主要获取下载内容的大小,可以显示下载进度多少字节
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders {
//获取 请求对象的标记
int bookid = [[request.userInfo objectForKey:@"bookID"] intValue];
//文件开始下载
}
//下载中
- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(longlong)bytes {
//获取 请求对象的标记
//下载中
}
//ASIHTTPRequestDelegate,下载完成时,执行的方法
- (void)requestFinished:(ASIHTTPRequest *)request {
int bookid = [[request.userInfoobjectForKey:@"bookID"]intValue];
//下载完成设置文件防止备份到iCloud和iTunes上
NSString *fileName = [request.userInfoobjectForKey:@"fileName"];
NSString *savePath = [FileSavePath stringByAppendingPathComponent:[NSStringstringWithFormat:@"DownLoad/%@.mp3",fileName]];
NSURL *url = [NSURLfileURLWithPath:savePath];
[selfaddSkipBackupAttributeToItemAtURL:url];
if ([self.delegaterespondsToSelector:@selector(DownLoadManageaSIdidFinishDownLoadByid:)]) {
[self.delegateDownLoadManageaSIdidFinishDownLoadByid:bookid];
}
}
- (void)requestFailed:(ASIHTTPRequest *)request {
//获取 请求对象的标记
int bookid = [[request.userInfoobjectForKey:@"bookID"]intValue];
//文件下载失败
}
//下载完成设置文件 防止备份到iCloud和iTunes上
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
assert([[NSFileManagerdefaultManager] fileExistsAtPath: [URLpath]]);
NSError *error = nil;
BOOL success = [URLsetResourceValue: [NSNumbernumberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKeyerror: &error];
if(!success){
VSLog(@"Error excluding %@ from backup %@", [URLlastPathComponent], error);
}else {
VSLog(@"防止备份属性设成功");
}
return success;
}
- iOS 利用 ASI 实现的异步队列下载文件
- iOS 之asi 框架 文件 的上传与下载
- IOS---利用ASIHTTPRequest实现异步队列
- ios异步中实现按序下载队列
- ASI-文件下载
- ios开发网络篇 - 使用ASI框架进行文件下载
- ASI实现文件断点续传
- 利用WebClient异步下载文件
- ASI使用介绍(文件下载)
- iOS 使用ASI实现图片的缓存机制
- iOS 使用ASI实现图片的缓存机制
- 利用Servlet实现文件的下载
- 利用struts2实现文件的下载
- AsyncTask异步实现文件下载
- 实现异步多线程下载文件
- AsyncTask异步实现文件下载
- iOS-利用AFNetworking(AFN 1.x)-实现文件断点下载
- iOS- 利用AFNetworking(AFN) 1.1.0- 实现文件断点下载
- 当按下手机back及home键时activity的状态变化及service中onStartCommand用法
- cmake编译安装mysql
- JAVA的构造函数
- 设计模式学习笔记01
- C++语言的表达式模板:表达式模板的入门性介绍
- iOS 利用 ASI 实现的异步队列下载文件
- 代码写入分配状态(工单)
- Arm-Linux根文件系统网络配置小结
- 如果我来做项目管理
- 【插件】插件的编写和使用
- 电子建档想法
- Improve efficiency
- C语言中函数参数的入栈顺序
- IPVS源代码分析---hook函数