Search code examples
iosipadasihttprequest

ASIHTTPRequest set Download Destination Path failing


I have a method which looks for a certain pdf, if it doesn't find it locally it uses ASIHTTPRequest to download it asynchronously. However the request always fails when the line: [request setDownloadDestinationPath:currentDetailItem]; is uncommented, the request starts and the progress increases until 100% then the request failed block is executed.

These are the relevant NSLogs when the request fails:

2012-08-16 12:08:34.398 XXXX[1675:707] Request started with url :http://XXXX.com/gwas/sites/default/files/Responding%20to%20Severe%20Weather%20Events.pdf
filePath :/var/mobile/Applications/322C24CF-9664-403D-9CC5-13C396F39F84/Documents/Responding%20to%20Severe%20Weather%20Events.pdf
2012-08-16 12:08:39.018 XXXX[1675:707] Request failed:HTTP/1.1 200 OK

Here is the code for the method:

- (void)setDetailItem:(NSString *)newDetailItem {
    NSArray *downloadPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSArray *components = [newDetailItem componentsSeparatedByString:@"/"];
    NSString *filePath = [[downloadPaths objectAtIndex:0] stringByAppendingFormat:@"/%@", [components lastObject]];
    currentDetailItem = filePath;

    if (![self fileExistsLocally:[components lastObject]]) {
        //Download the file
        [self displayProgressView];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:newDetailItem]];
        [request setDownloadDestinationPath:currentDetailItem];
        [request setDownloadProgressDelegate:progressBar];
        [request setCompletionBlock:^
         {
             [self showPdf:currentDetailItem];
             [self hideProgressView];
             NSLog(@"%f, Request finished :%@", progressBar.progress, request.responseStatusMessage);
         }];
        [request setFailedBlock:^
         {
             NSLog(@"Request failed:%@", request.responseStatusMessage);
             [self hideProgressView];
             [SVProgressHUD showErrorWithStatus:@"Request failed"];
         }];
        [request startAsynchronous];

        NSLog(@"Request started with url :%@\nfilePath :%@", newDetailItem, currentDetailItem);
    }
    else {
        [self showPdf:currentDetailItem]; 
    }
}

If I comment the line [request setDownloadDestinationPath:currentDetailItem]; out, the request is successful. Any ideas? thanks!


Solution

  • For anyone who is interested, I fixed the problem by swapping over to NSURLConnection, here's the code:

    - (void)setDetailItem:(NSString *)newDetailItem {
        NSArray *downloadPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSArray *components = [newDetailItem componentsSeparatedByString:@"/"];
        NSString *filePath = [[downloadPaths objectAtIndex:0] stringByAppendingFormat:@"/%@", [components lastObject]];
        currentDetailItem = filePath;
    
        if (![self fileExistsLocally:[components lastObject]]) {
            [self displayProgressView];
            data_ = [[NSMutableData alloc] init];
            NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:newDetailItem] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
            NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];
            if (!connection) {
                [SVProgressHUD showErrorWithStatus:@"Request failed"];
                return;
            }
            else {
                data_ = [[NSMutableData alloc] init];
            }
        }
        else {
            [self showPdf:currentDetailItem]; 
        }
    }
    
    #pragma mark NSURLConnectionDelegate methods
    
    - (void)connection: (NSURLConnection*) connection didReceiveResponse: (NSHTTPURLResponse*) response
    {
        if ([response statusCode] == 200) {
            currentDownloadSize = [response expectedContentLength];
        }
        [data_ setLength:0];
    }
    
    - (void) connection: (NSURLConnection*) connection didReceiveData: (NSData*) data
    {
        [data_ appendData:data];
        progressBar.progress = ((float) [data_ length] / (float) currentDownloadSize);
    
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        //Save file locally
        NSURL *url = [NSURL fileURLWithPath:currentDetailItem];
        NSError *error = nil;
        [data_ writeToURL:url options:0 error:&error];
        if (error) {
            NSLog(@"Write failed with error:%@", error);
        }
        else {
            NSLog(@"Request successful");
            [self showPdf:currentDetailItem];
        }
    
        [self hideProgressView];
    }
    
    - (void)connection:(NSURLConnection *)connection
      didFailWithError:(NSError *)error
    {
        [self hideProgressView];
        [SVProgressHUD showErrorWithStatus:@"Request failed"];
        NSLog(@"Connection failed! Error - %@ %@",
              [error localizedDescription],
              [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    }