【发布时间】: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