【问题标题】:Move file from Documents to tmp XCode 8 iOS 10将文件从 Documents 移动到 tmp XCode 8 iOS 10
【发布时间】:2017-02-28 22:02:35
【问题描述】:

我正在将我的应用程序从 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。 尽管进行了这种编辑,但 move 方法总是失败并出现以下错误:

Error Domain=NSCocoaErrorDomain Code=4 "“XXXX.pdf”无法移动到“tmp”,因为前者不存在,或者包含后者的文件夹不存在。" 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=( 移动 ), 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 "没有这样的文件或目录"}}

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

【问题讨论】:

    标签: ios xcode8 ios10 nsfilemanager


    【解决方案1】:

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

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

    这段代码运行良好,但我想了解为什么前一个代码不行。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-01
        • 2015-05-08
        • 1970-01-01
        相关资源
        最近更新 更多