私のアプリケーションをXCode7とiOS 9.xからXCode8とiOS10に移植しています。 私はファイルの処理に苦労しています。ドキュメントをtmpに移動XCode 8 iOS 10
ファイルをバックエンドからダウンロードしてから、/Documents
から/tmp
に移動する必要があります。ここに私のコードは次のとおりです。iOS10のいずれかで実行したときに、私は私のiOS 9.3シミュレータでアプリケーションが、アプリのクラッシュを実行する場合
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];
...
}
}];
すべてが正常に動作します。 最初に行った変更は、filePath
の代わりにmoveItemAtPath
メソッドfilePath.absoluteString
に渡すことです。 この編集にもかかわらず、移動方法は、常にこのエラーで失敗します。
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"}}
は、誰もがすでにエラーのようなものを扱っていますか?
NSError* errorWrite = nil;
NSData* data = [NSData dataWithContentsOfURL:filePath];
BOOL write = [data writeToFile:tmpPDFPath options:NSDataWritingAtomic error:&errorWrite];
このコードは正常に動作しますが、私は以前のものにはない理由を理解したいと思います: