【问题标题】:Saved files no longer exists after closing the app关闭应用后保存的文件不再存在
【发布时间】:2015-06-12 10:46:43
【问题描述】:

我正在使用 NSURLSessionDownloadTask 下载文件。它被下载并保存。并且可以正常显示数据。

不幸的是,我只有 iOS 模拟器可以测试。如果我用 Xcode 上的停止按钮关闭应用程序会发生什么。然后我重新启动,文件不再存在。

但是,如果我通过从应用程序中删除它来关闭它。通过单击 cmd + shift + H 两次运行列表。并通过点击应用程序重新启动它。在模拟器上。我找到了文件。

BOOL found = [[NSFileManager defaultManager] fileExistsAtPath:path];

这是模拟器问题吗?我不应该在真实设备上担心它吗?

实际上,我只是设法在 iPhone 上进行了测试。完全相同的行为!任何解释。

我在 NSURLSessionDownloadTask 上调用它,我发送了一个目标块,返回要保存的目标:

- (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request
                                             progress:(NSProgress * __autoreleasing *)progress
                                          destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination
                                    completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler

目标块代码:

NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSLibraryDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];

【问题讨论】:

    标签: ios ios-simulator afnetworking-2 nslibrarydirectory


    【解决方案1】:

    尝试登录documentsDirectoryURL,您会发现每次启动应用程序时都会有所不同。解决方案是在启动之间保存不是绝对 URL,而是相对于 NSLibraryDirectory 目录的路径。我遇到了同样的问题,我用两种方法解决了:

    // [self uploadFolder] returns a folder in NSLibraryDirectory
    + (NSString*)relativePathForURL:(NSURL*)url
    {
        NSURL *uploadFolderURL = [self uploadFolder];
        if ([url.baseURL isEqual:uploadFolderURL])
        {
            return url.relativeString;
        }
        else if ([url.absoluteString hasPrefix:uploadFolderURL.absoluteString])
        {
            return [url.absoluteString substringFromIndex:uploadFolderURL.absoluteString.length];
        }
        return nil;
    }
    
    + (NSURL*)urlForRelativePath:(NSString*)path
    {
        return [NSURL URLWithString:path relativeToURL:[self uploadFolder]];
    }
    

    它们应该以下列方式使用:

    1. 下载文件并将其移动到带有一些 URL 的 NSLibraryDirectory 文件夹 savedURL

    2. 拨打relativePath = [self relativePathForURL:savedURL]并保存。

    3. 应用重新启动时调用savedURL = [self urlForRelativePath:relativePath]

    4. savedURL 现在有效。

    【讨论】:

    • 它已经被保存到 NSDocumentDirectory。我更新了问题,请检查
    • @hasan83 感谢您的澄清。我已经更新了答案。
    • 谢谢你。即使我没有测试或使用你的解决方案。但我知道在每次运行中 LibraryDirectory 路径都会发生变化。我的错误是我将文件的完整路径保存在 userdefaults 中,并在需要时从那里获取。正如你提到的,我只应该将 LibraryDirectory 部分之后的部分保存在完整路径(RelativePath)中。
    【解决方案2】:

    这就是解决方案;

     - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
    
        //location is the temporary "destination" which you returned (can be Temp directory) and it now contains the downloaded file. Now copy the file to a new location (eg documents directory) for future use.
    
         BOOL fileCopied = [[[NSFileManager defaultManager] copyItemAtURL:location toURL:documentsDirectoryURLForSavingFileForFutureUe error:&error];
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      相关资源
      最近更新 更多