AssetsManager 在ios更新失败解决方案

AssetsManager在安卓平台使用正常,但是到ios就不行了,最后发现是 cocos2d\cocos\network\CCDownloader-apple.mm中的

- (void)URLSession:(NSURLSession *)session task :(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error 函数有问题,代码如下:

- (void)URLSession:(NSURLSession *)session task :(NSURLSessionTask *)task
                            didCompleteWithError:(NSError *)error
{
    DLLOG("DownloaderAppleImpl task: \"%s\" didCompleteWithError: %d errDesc: %s"
          , [task.originalRequest.URL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding]
          , (error ? (int)error.code: 0)
          , [error.localizedDescription cStringUsingEncoding:NSUTF8StringEncoding]);

    // clean wrapper C++ object
    DownloadTaskWrapper *wrapper = [self.taskDict objectForKey:task];
    
    // if no error, callback has been called in finish task
    if (_outer && error)
    {
        std::vector<unsigned char> buf; // just a placeholder
        _outer->onTaskFinish(*[wrapper get],
                             cocos2d::network::DownloadTask::ERROR_IMPL_INTERNAL,
                             (int)error.code,
                             [error.localizedDescription cStringUsingEncoding:NSUTF8StringEncoding],
                             buf);
    }
    [self.taskDict removeObjectForKey:task];
    [wrapper release];
}

 如果error为空的话就不会调用_outer->onTaskFinish,好坑爹啊,最后参考新版代码修改如下:

- (void)URLSession:(NSURLSession *)session task :(NSURLSessionTask *)task
                            didCompleteWithError:(NSError *)error
{
    DLLOG("DownloaderAppleImpl task: \"%s\" didCompleteWithError: %d errDesc: %s"
          , [task.originalRequest.URL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding]
          , (error ? (int)error.code: 0)
          , [error.localizedDescription cStringUsingEncoding:NSUTF8StringEncoding]);

    // clean wrapper C++ object
    DownloadTaskWrapper *wrapper = [self.taskDict objectForKey:task];
    
    // if no error, callback has been called in finish task
    if (_outer)
    {
        if(error)
        {
            std::vector<unsigned char> buf; // just a placeholder
            _outer->onTaskFinish(*[wrapper get],
                                 cocos2d::network::DownloadTask::ERROR_IMPL_INTERNAL,
                                 (int)error.code,
                                 [error.localizedDescription cStringUsingEncoding:NSUTF8StringEncoding],
                                 buf);
        }
        else if (![wrapper get]->storagePath.length())
        {
            // call onTaskFinish for a data task
            // (for a file download task, callback is called in didFinishDownloadingToURL)
            std::string errorString;
            
            const int64_t buflen = [wrapper totalBytesReceived];
            char buf[buflen];
            [wrapper transferDataToBuffer:buf lengthOfBuffer:buflen];
            std::vector<unsigned char> data(buf, buf + buflen);
            
            _outer->onTaskFinish(*[wrapper get],
                                 cocos2d::network::DownloadTask::ERROR_NO_ERROR,
                                 0,
                                 errorString,
                                 data);
        }
        else
        {
            NSInteger statusCode = ((NSHTTPURLResponse*)task.response).statusCode;
            
            // Check for error status code
            if (statusCode >= 400)
            {
                std::vector<unsigned char> buf; // just a placeholder
                const char *originalURL = [task.originalRequest.URL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding];
                char cmsg[256];
                sprintf(cmsg,"Downloader: Failed to download %s with status code (%d)", originalURL, (int)statusCode);
                std::string errorMessage = cmsg;
                
                _outer->onTaskFinish(*[wrapper get],
                                     cocos2d::network::DownloadTask::ERROR_IMPL_INTERNAL,
                                     0,
                                     errorMessage,
                                     buf);
            }
        }
    }
    [self.taskDict removeObjectForKey:task];
    [wrapper release];
}

 

posted @ 2018-04-26 18:26  冷夜 - 网游编程技术  阅读(642)  评论(0编辑  收藏  举报