将文件从 Documents 移动到 tmp XCode 8 iOS 10

Move file from Documents to tmp XCode 8 iOS 10

我正在将我的应用程序从 XCode7 和 iOS 9.x 移植到 XCode8 和 iOS10. 我正在努力处理文件。

我需要从我的后端下载一个文件,然后将其从 /Documents 移动到 /tmp。这是我的代码:

    AFURLSessionManager *manager = ...

    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
        return [documentsDirectoryPath URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        if(error) {
            ...
        } else {

            NSFileManager *fileManager = [NSFileManager defaultManager];

            NSError *error;
            NSString *tmpDirectory = NSTemporaryDirectory();
            NSString *tmpPDFPath = [tmpDirectory stringByAppendingPathComponent:[[response suggestedFilename] stringByReplacingOccurrencesOfString:@" " withString:@""]];

            if ([fileManager fileExistsAtPath:tmpPDFPath] == YES) {
                [fileManager removeItemAtPath:tmpPDFPath error:&error];
            }

            NSLog(@"readable %d", [fileManager isReadableFileAtPath:filePath]);
            // Print TRUE
            NSLog(@"tmpWritable %d", [fileManager isWritableFileAtPath:[NSURL URLWithString:tmpDirectory]]);
            // Print TRUE

            BOOL move = [fileManager moveItemAtPath:filePath toPath:tmpPDFPath error:&error];

            ...
        }
    }];

如果我在 iOS 9.3 模拟器中 运行 我的应用程序一切正常,但是当 运行 在 iOS10 模拟器中时应用程序崩溃。 我必须做的第一个更改是传递给 moveItemAtPath 方法 filePath.absoluteString 而不是 filePath。 尽管进行了这种编辑,移动方法总是失败并出现此错误:

Error Domain=NSCocoaErrorDomain Code=4 "“XXXX.pdf” couldn’t be moved to “tmp” because either the former doesn't exist, or the folder containing the latter doesn't exist." UserInfo={NSSourceFilePathErrorKey=/file:/Users/XXX/Library/Developer/CoreSimulator/Devices/24CAB2B2-F495-4CFF-90A7-5C51AF38C194/data/Containers/Data/Application/3D8EEEF9-F639-4D6C-BD5E-17A571F7B836/Documents/XXXX.pdf, NSUserStringVariant=( Move ), NSFilePath=/file:/Users/XXXX/Library/Developer/CoreSimulator/Devices/24CAB2B2-F495-4CFF-90A7-5C51AF38C194/data/Containers/Data/Application/3D8EEEF9-F639-4D6C-BD5E-17A571F7B836/Documents/“XXXX.pdf, NSDestinationFilePath=/Users/“XXXX/Library/Developer/CoreSimulator/Devices/24CAB2B2-F495-4CFF-90A7-5C51AF38C194/data/Containers/Data/Application/3D8EEEF9-F639-4D6C-BD5E-17A571F7B836/tmp/“XXXX.pdf, NSUnderlyingError=0x7b0a3500 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

有没有人处理过这种错误?

我的第一个解决方法是通过 NSData:

 NSError* errorWrite = nil;
 NSData* data = [NSData dataWithContentsOfURL:filePath];
 BOOL write = [data writeToFile:tmpPDFPath options:NSDataWritingAtomic error:&errorWrite];

此代码工作正常,但我想了解为什么前一个代码不工作。

我遇到了同样的问题,我的解决方案是从 3.0.15 更新到最新版本的 Dropbox SDK(当前为 3.2.0)。特别是,我仍然以 iOS 8.x 为目标,所以我的 CocoaPods 更新没有选择较新版本的 Dropbox,并且此错误已在 3.0.18 版左右修复。有关详细信息,请参阅 this forum QA