didcompletewitherror即使对于有坏网址的请求也会返回nil

时间:2016-08-05 14:33:23

标签: ios objective-c nsurlsession nsurl nsurlsessionconfiguration

我正在运行didcompletewitherror我用来下载本地托管的XML文件:url - http://10.232.254.59/catalogue.xml

问题在于我使用网址 - http://10.232.254.59/catalogue.x或 url - http://10.232.254.59/catal或任何其他组合而不更改IP,// 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; } 委托给我错误=(null)。

didCompleteWithError

我在更改IP时didFinishDownloadingToURL出现错误但在更改IP / *时没有,因此即使出现错误的网址,也会显示下载成功提示。
即使在网址不好的情况下,我也会收到LoadMediaUrl_StartTask_StartActivityIndicator代表电话 我在几个地方调用<body> <div class="fadeInDown"> I want this to appear centered, from the top and over all my content </div> <div id='content'> ...much complex html layout...bootstrap...wow </div> </body> 并使用相同的backgroundSession obj。

有谁能告诉我可能是什么原因?

2 个答案:

答案 0 :(得分:0)

我想我得到了原因 URLSession didCompleteWithError nil error

它因为它只报告客户端错误,但不确定这种风格如何符合服务器问题。

答案 1 :(得分:0)

要检测服务器错误(例如404),请实现didReceiveResponse委托方法。从那里,检查NSHTTPURLResponse对象中的httpStatusCode属性。您必须首先将NSURLResponse强制转换为NSHTTPURLResponse指针。