我有一个Nsurlsesiondownloadtask
运行,用于下载本地托管的 XML 文件:url - http://10.232.254.59/catalogue.xml
问题是当我使用 url - http://10.232.254.59/catalogue.x或 url - http://10.232.254.59/catal 或任何其他组合而不更改 IP 时,didcompletewitherror
委托给我 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;
}
我在更改 IP 时得到正确的错误,didCompleteWithError
但在更改 IP/* 时却没有,因为即使在错误 url 的情况下,也会显示下载成功警报。
即使在错误的 url 情况下,我也会收到didFinishDownloadingToURL
委托呼叫。
我LoadMediaUrl_StartTask_StartActivityIndicator
在其他几个地方打电话并使用相同的 backgroundSession obj。
谁能告诉我可能是什么原因?