Iphone ASIHTTPRequest多个下载控件,如暂停、恢复
我正在使用ASIHTTPRequest从后台的URL下载视频文件 我用进度条和百分比显示下载,我希望用户可以控制下载,如暂停和恢复 代码如下:Iphone ASIHTTPRequest多个下载控件,如暂停、恢复,iphone,objective-c,ios,asihttprequest,download-manager,Iphone,Objective C,Ios,Asihttprequest,Download Manager,我正在使用ASIHTTPRequest从后台的URL下载视频文件 我用进度条和百分比显示下载,我希望用户可以控制下载,如暂停和恢复 代码如下: -(void)Initiate_Download:(NSString*)urlStr contentID:(NSString*)cid progressBar:(UIProgressView*)progressBar { NSLog(@"Initiate_Download for cid:%@",cid); urlStr = [u
-(void)Initiate_Download:(NSString*)urlStr contentID:(NSString*)cid progressBar:(UIProgressView*)progressBar
{
NSLog(@"Initiate_Download for cid:%@",cid);
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
NSString *fileName = [NSString stringWithFormat:@"%@.mp4",cid];
NSString *destinationPath = [[self VideoDownloadFolderPath]stringByAppendingPathComponent:fileName];
[request setDownloadDestinationPath:destinationPath];
[request setTemporaryFileDownloadPath:[NSString stringWithFormat:@"%@-part",destinationPath]];
[request setDelegate:self];
NSDictionary *rqstDict = [NSDictionary dictionaryWithObjectsAndKeys:cid,@"cid",urlStr,@"url", nil];
[request setUserInfo:rqstDict];
[request setAllowResumeForFileDownloads:YES];
[request startAsynchronous];
}
//Delegate
- (void)requestStarted:(ASIHTTPRequest *)request1
{
//some code
}
- (void)request:(ASIHTTPRequest *)request1 didReceiveResponseHeaders:(NSDictionary *)responseHeaders
{
//some code
}
- (void)requestFinished:(ASIHTTPRequest *)request1
{
//some code
}
- (void)requestFailed:(ASIHTTPRequest *)request1
{
//some code
}
您需要为每个请求保存请求的URL和目标路径,并暂停请求使用代码:- [请求取消] 要恢复请求,您需要创建另一个具有相同URL和目标路径的请求。例如:-
ASIHTTPRequest *requestToResume = [ASIHTTPRequest requestWithURL:url];
[requestToResume setTemporaryFileDownloadPath:tempfilePath];
[requestToResume setDownloadDestinationPath:filePath];
[requestToResume setDelegate:self];
[requestToResume setDownloadProgressDelegate:self];
[requestToResume setUserInfo:dictInfo];
// This file has part of the download in it already
[requestToResume setAllowResumeForFileDownloads:YES];
[requestToResume setDidFinishSelector:@selector(requestDone:)];
[requestToResume setDidFailSelector:@selector(requestWentWrong:)];
[requestToResume startAsynchronous];
在上面的代码中,我们从字典中获取歌曲的url,该字典被设置为请求的userInfo,现在我们获取恢复请求的这些详细信息。当我们恢复请求时,文件将从暂停点下载,因此它将解决恢复文件下载的目的。通过保存url和目标路径,如何执行[request cancel]命令。我可以保存请求对象吗?如果是的话,怎么办?然后[请求取消]命令是否有效?您必须将请求对象保存在一个数组中,并为每个请求提供一个唯一的标记,并在表中显示请求,当用户希望暂停或恢复特定请求时,您可以从数组中获取请求的标记,您可以通过从表中获取的标记找到请求,然后使用我上面提到的代码。我试图在[defaults setObject:request forKey:@“object”中保存一个HttpRequest请求对象;但它崩溃了。您能告诉我如何保存ASIHTTP对象吗?为什么要将其保存在userdefaults中?将其保存在一个数组中,该数组将在生成或完成请求或发生错误时更新。