0

I am using ASIHTTPRequest to download video file from URL in background.

I am displaying the downloads with progress-bar & percentage and I want user can control the downloads like pause & resume.

Below is the code:

 -(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
}
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
swati sharma
  • 367
  • 4
  • 15

1 Answers1

1

You need to save the URL and destination path of the request for each request and to pause the request use code :-

[request Cancel];

and to resume the request you need to create another request with same URL and destination path. For example :-

    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];

In the above code we get the url of the song from the dictionary which was set as userInfo of the request and now we get these details for resuming the request. When we resume the request the file will be downloaded from the point it was paused, hence it will solve the purpose of resuming the file download.

Chirag Bhutani
  • 229
  • 1
  • 8
  • By saving url & destination path, how can I execute [request cancel] command. – swati sharma Dec 07 '12 at 07:29
  • Can I save the request object? If yes, then How? and then [request cancel] command will work? – swati sharma Dec 07 '12 at 07:31
  • You would have to save the request object in an Array and give each request a unique tag and show the requests in a table in a way that when the user wishes to pause or resume a specific request you are able to get the tag of the request and from the array you can find the request by the tag that you got from the table and then use the code that i mentioned above. – Chirag Bhutani Dec 07 '12 at 07:43
  • I tried to save ASIHTTPRequest request object in [defaults setObject:request forKey:@"object"]; But it crashed. Can you tell me how to save ASIHTTP object? – swati sharma Dec 07 '12 at 08:16
  • Why do you want to save it in userdefaults? save it in an Array which will be updated whenever a requested is generated or completed or some error occurs. – Chirag Bhutani Dec 07 '12 at 09:42
  • It is crashing (SIGABRT) while saving in NSMutableArry. Can you tell me how to save ASI objects? – swati sharma Dec 07 '12 at 11:41
  • It should not crash.Can you post the code for creating the request and adding it to the array. Did you initialize the Array before adding the request object? – Chirag Bhutani Dec 07 '12 at 12:22