Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSURLSessionDownloadTask-下载,但以错误结束_Objective C_Nsurlsession - Fatal编程技术网

Objective c NSURLSessionDownloadTask-下载,但以错误结束

Objective c NSURLSessionDownloadTask-下载,但以错误结束,objective-c,nsurlsession,Objective C,Nsurlsession,我正在尝试下载一个pdf文件。早些时候,当我使用完成处理程序块时,我能够在tmp位置看到该文件。然后我想显示下载进度,所以我实现了委托方法。但我现在可以看到进度条在工作,文件正在下载。但是一旦下载完成(写入字节数/总字节数=1),就会调用错误委托,并且tmp位置中没有文件。我错过了什么?下面是我的代码。我已经上传了项目在 在didfishdownloadingtourl中,您应该将文件从位置移动到更永久的位置(例如,您的文档文件夹)。如果您以后在临时位置查找该文件,我并不奇怪它不再存在 如上所述

我正在尝试下载一个pdf文件。早些时候,当我使用完成处理程序块时,我能够在tmp位置看到该文件。然后我想显示下载进度,所以我实现了委托方法。但我现在可以看到进度条在工作,文件正在下载。但是一旦下载完成(写入字节数/总字节数=1),就会调用错误委托,并且tmp位置中没有文件。我错过了什么?下面是我的代码。我已经上传了项目在


didfishdownloadingtourl
中,您应该将文件从
位置
移动到更永久的位置(例如,您的文档文件夹)。如果您以后在临时位置查找该文件,我并不奇怪它不再存在

如上所述,
位置
的定义如下:

临时文件的文件URL。由于该文件是临时文件,因此在从此委托方法返回之前,您必须打开该文件进行读取,或者将其移动到应用程序沙盒容器目录中的永久位置


您必须先将文件移动到新位置,然后才能从
didfishdownloadingtourl

@Rob感谢您的及时回复,这对我帮助很大。这是我的代码。希望它能帮助别人。我能够获得实际的文件名,并使用原始名称将文件移动到我的文档目录

-(void) URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;

    //getting application's document directory path
    NSArray * tempArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [tempArray objectAtIndex:0];

    //adding a new folder to the documents directory path
    NSString *appDir = [docsDir stringByAppendingPathComponent:@"/Reader/"];

    //Checking for directory existence and creating if not already exists
    if(![fileManager fileExistsAtPath:appDir])
    {
        [fileManager createDirectoryAtPath:appDir withIntermediateDirectories:NO attributes:nil error:&error];
    }

    //retrieving the filename from the response and appending it again to the path
    //this path "appDir" will be used as the target path 
    appDir =  [appDir stringByAppendingFormat:@"/%@",[[downloadTask response] suggestedFilename]];

    //checking for file existence and deleting if already present.
    if([fileManager fileExistsAtPath:appDir])
    {
        NSLog([fileManager removeItemAtPath:appDir error:&error]?@"deleted":@"not deleted");
    }

    //moving the file from temp location to app's own directory
    BOOL fileCopied = [fileManager moveItemAtPath:[location path] toPath:appDir error:&error];
    NSLog(fileCopied ? @"Yes" : @"No");

}

万一有人和我有同样的问题,我想我会在这里发布我的解决方案

我的问题是谓词方法在后台线程上触发,所以我将其分派到我的“文件io”线程,该线程处理应用程序中的任何文件写入、删除等操作

这样做的问题是,当委托方法结束时,临时文件就会被删除,而这正是我切换线程时发生的。因此,当我试图访问我的文件io线程中的文件时,它已被删除


我的解决方案是使用委托方法将文件解析为NSData,然后使用NSData在我的文件io线程中写入文件系统。

我正在调试应用程序,下载完成后,tmp文件夹中的文件就会丢失(我不会将其移到任何地方)。在下载过程开始之前,我可以在tmp文件夹中看到一个临时文件。@泰米尔语我已经用文档中的参考更新了我的答案,该参考鼓励您在退出
didfishdownloadingtourl
之前将文件移到永久位置。操作系统可能会在您从委托方法返回后立即将其删除。您好@Rob非常感谢,这很有效。但我现在在复制文件时遇到了问题。我在documents目录中创建了一个目录,并尝试使用[fileManager copyItemAtPath:[location absoluteString]toPath:newPath error:&error]将文件移动到那里,但这返回NO,并且文件未被复制:(@Tamil个人,我将使用
[fileManager copyItemAtURL:location toURL:[NSURL fileURLWithPath:newPath]错误:&error]
。但是您是否查看了
错误
对象?这应该会告诉您出了什么问题。@SmartHome-1.是的,以后请发布您自己的问题。2.您的错误告诉您的不是旧文件已被删除,而是所选名称的文件已存在于所需的目标中。3.您确实应该执行移动操作调用该队列的队列上的文件(或者如果出于同步原因需要另一个队列,则同步调度)。4.移动文件(与复制文件不同)发生得非常快,因此不必担心性能问题。5.如果您需要另一个队列,请在设置会话时指定该队列。您登录时出现的错误是什么
didcompletewitheror
?当我打印
[error localizedDescription]
时,我得到“操作无法完成”(Cocoa错误516)。”。我可以通过在
toPath
的末尾添加一个文件名来解决这个问题,现在它起作用了。有没有其他有效的方法来做到这一点?我不太清楚,但可能只是生成一个UUID,因为文件名会起作用……这个答案确实需要一些说明和示例。
-(void) URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;

    //getting application's document directory path
    NSArray * tempArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [tempArray objectAtIndex:0];

    //adding a new folder to the documents directory path
    NSString *appDir = [docsDir stringByAppendingPathComponent:@"/Reader/"];

    //Checking for directory existence and creating if not already exists
    if(![fileManager fileExistsAtPath:appDir])
    {
        [fileManager createDirectoryAtPath:appDir withIntermediateDirectories:NO attributes:nil error:&error];
    }

    //retrieving the filename from the response and appending it again to the path
    //this path "appDir" will be used as the target path 
    appDir =  [appDir stringByAppendingFormat:@"/%@",[[downloadTask response] suggestedFilename]];

    //checking for file existence and deleting if already present.
    if([fileManager fileExistsAtPath:appDir])
    {
        NSLog([fileManager removeItemAtPath:appDir error:&error]?@"deleted":@"not deleted");
    }

    //moving the file from temp location to app's own directory
    BOOL fileCopied = [fileManager moveItemAtPath:[location path] toPath:appDir error:&error];
    NSLog(fileCopied ? @"Yes" : @"No");

}