Skip to content

Commit ffcf45e

Browse files
committed
Improved solution for Downloader status code issue on iOS 9
1 parent 7303895 commit ffcf45e

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

cocos/network/CCDownloader-apple.mm

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -389,32 +389,56 @@ - (void)URLSession:(NSURLSession *)session task :(NSURLSessionTask *)task
389389

390390
if(_outer)
391391
{
392-
if(error) {
392+
if(error)
393+
{
393394
std::vector<unsigned char> buf; // just a placeholder
394395
_outer->onTaskFinish(*[wrapper get],
395396
cocos2d::network::DownloadTask::ERROR_IMPL_INTERNAL,
396397
(int)error.code,
397398
[error.localizedDescription cStringUsingEncoding:NSUTF8StringEncoding],
398399
buf);
399400
}
400-
else if(![wrapper get]->storagePath.length()) {
401+
else if (![wrapper get]->storagePath.length())
402+
{
401403
// call onTaskFinish for a data task
402404
// (for a file download task, callback is called in didFinishDownloadingToURL)
403-
std::string errorString = cocos2d::StringUtils::format("Downloader: Failed to download %s with no error", [task.originalRequest.URL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding]);
404-
405+
std::string errorString;
406+
405407
const int64_t buflen = [wrapper totalBytesReceived];
406408
char buf[buflen];
407-
408409
[wrapper transferDataToBuffer:buf lengthOfBuffer:buflen];
409-
410410
std::vector<unsigned char> data(buf, buf + buflen);
411411

412412
_outer->onTaskFinish(*[wrapper get],
413413
cocos2d::network::DownloadTask::ERROR_NO_ERROR,
414-
0,
414+
0,
415415
errorString,
416416
data);
417417
}
418+
else
419+
{
420+
NSInteger statusCode = ((NSHTTPURLResponse*)task.response).statusCode;
421+
std::vector<unsigned char> buf; // just a placeholder
422+
const char *orignalURL = [task.originalRequest.URL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding];
423+
std::string errorMessage = "";
424+
425+
// Check for error status code
426+
if (statusCode >= 400)
427+
{
428+
errorMessage = cocos2d::StringUtils::format("Downloader: Failed to download %s with status code (%d)", orignalURL, (int)statusCode);
429+
}
430+
// Unknown issue
431+
else
432+
{
433+
errorMessage = cocos2d::StringUtils::format("Downloader: Failed to download %s with unknown error", orignalURL);
434+
}
435+
436+
_outer->onTaskFinish(*[wrapper get],
437+
cocos2d::network::DownloadTask::ERROR_IMPL_INTERNAL,
438+
0,
439+
errorMessage,
440+
buf);
441+
}
418442
}
419443
[self.taskDict removeObjectForKey:task];
420444
[wrapper release];
@@ -511,22 +535,20 @@ - (void)URLSession:(NSURLSession *)session downloadTask :(NSURLSessionDownloadTa
511535
return;
512536
}
513537

514-
DownloadTaskWrapper *wrapper = [self.taskDict objectForKey:downloadTask];
515-
const char * storagePath = [wrapper get]->storagePath.c_str();
516-
NSString *destPath = [NSString stringWithUTF8String:storagePath];
517-
NSFileManager *fileManager = [NSFileManager defaultManager];
518-
NSURL *destURL = nil;
519-
520538
// On iOS 9 a response with status code 4xx(Client Error) or 5xx(Server Error)
521539
// might end up calling this delegate method, saving the error message to the storage path
522540
// and treating this download task as a successful one, so we need to check the status code here
523541
NSInteger statusCode = ((NSHTTPURLResponse*)downloadTask.response).statusCode;
524-
if (statusCode >= 400) {
525-
std::vector<unsigned char> buf; // just a placeholder
526-
std::string response = cocos2d::StringUtils::format("Downloader: Failed to download %s with status code (%d)", [downloadTask.originalRequest.URL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding], (int)statusCode);
527-
_outer->onTaskFinish(*[wrapper get], cocos2d::network::DownloadTask::ERROR_IMPL_INTERNAL, noErr, response, buf);
542+
if (statusCode >= 400)
543+
{
528544
return;
529545
}
546+
547+
DownloadTaskWrapper *wrapper = [self.taskDict objectForKey:downloadTask];
548+
const char * storagePath = [wrapper get]->storagePath.c_str();
549+
NSString *destPath = [NSString stringWithUTF8String:storagePath];
550+
NSFileManager *fileManager = [NSFileManager defaultManager];
551+
NSURL *destURL = nil;
530552

531553
do
532554
{

0 commit comments

Comments
 (0)