gpt4 book ai didi

ios - 将文件下载到文档路径后,iOS AFNetworking 3.0文件不存在

转载 作者:行者123 更新时间:2023-12-01 22:18:05 24 4
gpt4 key购买 nike

我尝试使用AFnetworking从服务器下载json文件。
我遵循官方示例代码。
下载json后,我需要使用此json对其他本地对象执行操作。
当我用路径获取此json文件时,我总是得到nil。
下面是我的代码

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if(!error){
NSLog(@"File downloaded to: %@", filePath);

if([[NSFileManager defaultManager] fileExistsAtPath:filePath.absoluteString]){
NSLog(@"file exists");
} else {
NSLog(@"file not exists");
}

}else{
NSLog(@"download error %@",error);
}
}];
[downloadTask resume];

您会看到我添加了一个逻辑来检查文件是否存在。
请求始终成功,但不存在文件。
文件路径将以file:/// var / ...开头
我如何找回这个json文件?

最佳答案

调用filePath.path时尝试使用filePath.absoluteString代替fileExistsAtPath
FileManager需要传递一个文件URL

关于ios - 将文件下载到文档路径后,iOS AFNetworking 3.0文件不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50252199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com