Browse Source

correct handling iOS http error for download task

dmuratshin 9 years ago
parent
commit
3463f21f92
1 changed files with 19 additions and 7 deletions
  1. 19 7
      oxygine/src/core/ios/HttpRequestCocoaTask.mm

+ 19 - 7
oxygine/src/core/ios/HttpRequestCocoaTask.mm

@@ -42,22 +42,34 @@ static char taskKey;
 }
 }
 
 
 -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
 -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
+    
     oxygine::HttpRequestCocoaTask* task = [self getTask:downloadTask remove:true];
     oxygine::HttpRequestCocoaTask* task = [self getTask:downloadTask remove:true];
     
     
-    NSFileManager *fileManager = [NSFileManager defaultManager];
+    NSHTTPURLResponse *resp = (NSHTTPURLResponse*)[downloadTask response];
+    
+    int code = [resp statusCode];
     
     
     
     
+    NSFileManager *fileManager = [NSFileManager defaultManager];
+    
     std::string dest = oxygine::file::wfs().getFullPath(task->getFileName().c_str());
     std::string dest = oxygine::file::wfs().getFullPath(task->getFileName().c_str());
     NSURL *destUrl = [NSURL fileURLWithPath:[NSString stringWithUTF8String:dest.c_str()]];
     NSURL *destUrl = [NSURL fileURLWithPath:[NSString stringWithUTF8String:dest.c_str()]];
     
     
-    //NSURL *originalUrl = [NSURL URLWithString:[downloadTaskURL lastPathComponent]];
-    //NSURL *destinationUrl = [documentsDirectory URLByAppendingPathComponent:[originalUrl lastPathComponent]];
     NSError *fileManagerError;
     NSError *fileManagerError;
     
     
-    [fileManager removeItemAtURL:destUrl error:NULL];
-    //key line!
-    [fileManager copyItemAtURL:location toURL:destUrl error:&fileManagerError];
-    task->complete_(/* data */ nil, /* error */ false);
+    [fileManager removeItemAtURL:destUrl error:&fileManagerError];
+    
+    if (code == 200)
+    {
+        [fileManager copyItemAtURL:location toURL:destUrl error:&fileManagerError];
+    
+        task->complete_(/* data */ nil, /* error */ false);
+    }
+    else
+    {
+        task->complete_(nil, true);
+        
+    }
 }
 }
 
 
 #pragma mark - NSURLSessionTaskDelegate
 #pragma mark - NSURLSessionTaskDelegate