0

I have a Nsurlsesiondownloadtask running which I use to download an XML file locally hosted at say : url - http://10.232.254.59/catalogue.xml

Problem is when I use url - http://10.232.254.59/catalogue.x or url - http://10.232.254.59/catal or any other combination without changing the IP, didcompletewitherror delegate gives me error= (null).

//  MainViewController.m
@interface MainViewController ()
{   
    NSURLSessionDownloadTask *download;  
} 
@property(nonatomic,strong)NSURLSession *backgroundSession;

- (void)viewDidLoad {
  NSURLSessionConfiguration *ConfigurationObject = [NSURLSessionConfiguration defaultSessionConfiguration];
    ConfigurationObject.timeoutIntervalForRequest = 10.0;
    ConfigurationObject.timeoutIntervalForResource = 10.0;
    self.backgroundSession = [NSURLSession sessionWithConfiguration:ConfigurationObject delegate:self delegateQueue:nil];

    [self LoadMediaUrl_StartTask_StartActivityIndicator];
}
-(void)LoadMediaUrl_StartTask_StartActivityIndicator{
    NSURL *url ;
    url = [self mediaUrlFromSettings];
    if(url.absoluteString.length != 0){
        download = [self.backgroundSession downloadTaskWithURL:url];
        [download resume];
        --do more stuff---
    }     
}
- (void)URLSession:(NSURLSession *)session downloadTask:(nonnull NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(nonnull NSURL *)location{

NSLog(@"didFinishDownloadingToURL called");

NSFileManager *fileManager = [NSFileManager defaultManager];
dispatch_async(dispatch_get_main_queue(), ^{
*** start the parser ***
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Success" message:[NSString stringWithFormat:@" Catalogue Download Success! Press OK to Load New Media List."] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"cancel", nil];
    alert.tag = ALERT_VIEW_DOWNLOAD_SUCCESS;
    [alert show];
});


  -(void)URLSession:(NSURLSession *)session task:(nonnull NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error
{  

       NSLog(@"didCompleteWithError error :  %@",[error localizedDescription]);
    if(error != nil){

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.downloadActivityIndicator stopAnimating];
            [self.downloadProgressView setHidden:YES];
            self.DownloadImageView.alpha = 1.0;
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:[NSString stringWithFormat:@" Catalogue Download Failed ! %s%@","\n",[error localizedDescription]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Retry", nil];
            alert.tag =ALERT_VIEW_DOWNLOAD_FAILED;
            [alert show];
        });
    }

    download = nil;
}

I get proper error in didCompleteWithError when IP is changed but not when I change IP/* , because of which even in case of bad url , download success alert shows up.
I also receive the didFinishDownloadingToURL delegate call even in bad url case .
I am calling LoadMediaUrl_StartTask_StartActivityIndicator at few more places and using the same backgroundSession obj.

Could anyone please tell me what might be the cause ?

D4ttatraya
  • 3,344
  • 1
  • 28
  • 50
CodeTry
  • 312
  • 1
  • 19

2 Answers2

0

I think i got the cause URLSession didCompleteWithError nil error

Its because it reports only client side errors , but not sure how this scenarious qualifies for server problem.

Community
  • 1
  • 1
CodeTry
  • 312
  • 1
  • 19
  • Don't post followup questions in an answer. A posted answer should fully answer the question, not ask another question. – rmaddy Aug 05 '16 at 15:28
0

To detect server errors (e.g. 404), implement the didReceiveResponse delegate method. From there, check the httpStatusCode property in the NSHTTPURLResponse object. You'll have to cast the NSURLResponse to an NSHTTPURLResponse pointer first.

dgatwood
  • 10,129
  • 1
  • 28
  • 49