Javascript iOS应用程序在使用Phonegap下载文件时崩溃

Javascript iOS应用程序在使用Phonegap下载文件时崩溃,javascript,ios,objective-c,html,cordova,Javascript,Ios,Objective C,Html,Cordova,当使用phonegap下载文件时,当互联网关闭时,应用程序挂起并崩溃。我得到以下错误: *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[\uu NSCFDictionary setObject:forKey::]:尝试插入零值(键:body)' *第一次抛出调用堆栈: (0x35b4c88f 0x33770259 0x35b4c789 0x35b4c7ab 0x35ab968b 0x56a7f 0x57f8d 0x34f9dc29 0x34e

当使用phonegap下载文件时,当互联网关闭时,应用程序挂起并崩溃。我得到以下错误:

*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[\uu NSCFDictionary setObject:forKey::]:尝试插入零值(键:body)' *第一次抛出调用堆栈: (0x35b4c88f 0x33770259 0x35b4c789 0x35b4c7ab 0x35ab968b 0x56a7f 0x57f8d 0x34f9dc29 0x34ef56d9 0x34ef56a3 0x34f9ddfb 0x3189dcb5 0x318034b7 0x318031a3 0x30803D9 0x35b20ad3 0x35b20335 0x35b1f045 0x35aa24a5 35AA236D 0x3383D39 0x3327acd5 0x28d9f 0x28d60) 终止引发异常的调用(lldb)

正在下载文件的逻辑:

if(isOnline){
            //  alert("DEVICE is ONLINE" + isOnline);
            try{
                filePath = globalPathNew + "/" +name+ "."+fExt;
                //     alert("SAVING VIDEO AT ------> " + filePath);


                document.getElementById('PB'+name).style.display = 'block';

                document.getElementById('P'+name+'L').innerHTML = '';

                document.getElementById('P'+name+'L').innerHTML = 'Downloading...';





                ftv.onprogress = function(progressEvent) {
                    console.log("In Progress video"+progressEvent.lengthComputable);
                    if (progressEvent.lengthComputable) {
                        //downloadcompleteStatus =  Math.round(100 * (progressEvent.loaded / progressEvent.total));
                        $("#P" + name + "Progress").text(Math.round(100 * (progressEvent.loaded / progressEvent.total)) + "%");
                        console.log("------progressEvent if video------->%"+Math.round(100 * (progressEvent.loaded / progressEvent.total)));
                    } else {
                        console.log("------progressEvent else------->%"+Math.round(100 * (progressEvent.loaded / progressEvent.total)));

                    }
                };


                fileObjAbort = ftv;
                //alert("Video FileObj"+JSON.stringify(fileObjAbort));

                ftv.download(
                             url,
                             filePath,
                             function(entry) {

                             // alert("download complete: " + entry.fullPath);
                             //                                      console.log('video entry.fullPath------'+entry.fullPath);
                             //                                      console.log('video filePath ----'+filePath);

                             changeIsdownloadStatus(filePath, name, 'video');

                             if(currElementId != '' && currElementId != '' && currElementId != ''){
                             detailPageView(currElementId,currElementtype,currElementcountNum);
                             }
                             var index = -1;

                             $.each(downloadList, function(key,tempItem){
                                    if(tempItem.elementId == elementId){
                                    index = key;

                                    }
                                    });

                            // alert("dw list"+JSON.stringify(downloadList));
                             if(index != -1){
                             delete downloadList[index];
                             downloadList.splice(index,1);
                             }
                             //alert("dw list after Video Splice \n"+JSON.stringify(downloadList));
                             findNextDownloadItem(name);


                             //  alert("DOWNLOADED ------> ");
                             if(currDownload == elementId){

                             //changeIsdownloadStatus(filePath,name, 'delete');

                             if(currElementId != '' && currElementId != '' && currElementId != ''){
                             detailPageView(currElementId,currElementtype,currElementcountNum);
                             }

                             }


                             console.log('*********************Video******************************');
                             console.log('-->delete element id:'+elementId);
                             console.log('deleteProgressindex Position:'+index);
                             console.log('-->before delete Length Array :'+downloadList.length);

                             console.log('-->before delete Length Array :'+downloadList.length);
                             console.log('***************************************************');


                             //                                      playMedia(filePath, name,'video');


                             // Code added
                           //bhavya  getFileSystemRefForWriting(jsonData);

                             deleteProgress(name,elementTitle,isDownloadedFlag,elementAudio,val,filePath);
                             // Code added
                             // getFileSystemRefForWriting(jsonData);

                             },
                             function(error) {
                             if(error.code != 4 || error.code != '4'){
                             //alert("Download Error");

                             jAlert('Download was not completed due to lost internet connection. Please connect to the Internet and re-download.', 'TT');
                             console.log("download error" + error.source);
                             //                                      console.log("download error source " + error.source);
                             //                                      console.log("download error target " + error.target);
                             //                                      console.log("upload error code" + error.code);
                             }
                             }
                             );

            }
            catch(error)
            {
                var txt="There was an error on this page.\n\n";
                txt+="Error description: " + err.message + "\n\n";
                txt+="Click OK to continue.\n\n";
                console.log('in catch block of video------>>>>'+txt);
            }


        }else{
            jAlert('Please go online to download file.', 'TT');
        }
当我断开与internet的连接时,我会出现以下错误: 由于internet连接中断,下载未完成。请连接到Internet并重新下载

当互联网关闭时,切换到objective-c在main.m文件中进行,并卡在此行:

retVal为0。并且它不执行NSLog行

#import <UIKit/UIKit.h>

int main(int argc, char* argv[])
{
    @autoreleasepool {
        **int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");**

        NSLog(@"RETURN VALUE ----->: %d ", retVal);

        return retVal;
    }
}
#导入
int main(int argc,char*argv[])
{
@自动释放池{
**int retVal=UIApplicationMain(argc,argv,nil,@“AppDelegate”)**
NSLog(@“返回值------>:%d”,retVal);
返回返回;
}
}
有人能提出可能的原因吗

谢谢,
Ankit Tanna

不能将
nil
值设置为字典对象。如果您想为无值设置占位符,请使用
[NSNull null]
,这将创建singleton
NSNull
的实例。当它试图为键
主体定义对象时,它将崩溃。如果您有权访问代码,请尝试
try。。。。捕捉
以防止它尝试
nil
,这可能是一个默认值


如果没有代码,这只是从错误消息中猜测出来的。

下面是phonegap 2.5的修复程序

  • 转到CordovaLib.xcodeproject
  • 导航到:类>命令>CDVFileTransfer.m
  • 转到或搜索以下函数:-(NSMutableDictionary*)createFileTransferError
  • 在[result setObject:body forKey:@“body”]之前添加以下行 如果(body==nil)body=@”
  • 瞧!!!你在那里
  • 感谢我的同事:)


    PS:这些错误和其他几个错误都列在Phonegap for iOS的Apache页面上。

    您能提供更多详细信息吗。就像下载文件的代码片段,或者您是否在Phonegap应用程序中使用任何本机插件?好的,我将编辑问题并添加下载逻辑。调用不会进入错误处理函数。如果internet连接良好,是否会触发成功回调,并且错误回调的唯一问题出现?请检查ftv.download()函数中的“url”和“filePath”参数。一定有什么东西没有正确传递,或者任何变量没有初始化。并检查您在何处创建了“ftv”对象。您确定控件是否到达下载函数,并且在函数调用之前没有卡在某个位置吗?是的,错误回调出现问题。网络连接很好。这是我手动断开连接的情况。”“ftv”对象已创建。下载正在成功进行。它完全执行success函数。但是没有进入错误回调。嗨,我添加了下载逻辑。