私のアプリでは、ASiHttpRequestを含むPDFファイルをダウンロードし、次の手順を実行します。
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[currentDownload setDownloadDestinationPath:[documentsDirectory stringByAppendingPathComponent:@"file.pdf"]];
最初は正常に動作し、このfile.pdfを開くことができますが、このpdfを2回ダウンロードすると、ファイルが置き換えられるのではなく、マージされるようです。
私がこれを行う前に、それは問題がどこにあるのか、またはそのパスからこのfile.pdfを削除するための最良の方法は何ですか?
- (void) removeFile{
NSString *extension = @"pdf";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPDF = [paths objectAtIndex:0];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectoryPDF error:NULL];
NSEnumerator *e = [contents objectEnumerator];
NSString *filename;
while ((filename = [e nextObject])) {
if ([[filename pathExtension] isEqualToString:extension]) {
[fileManager removeItemAtPath:[documentsDirectoryPDF stringByAppendingPathComponent:filename] error:NULL];
}
}
}
編集
今私はこの方法を使用します
- (void) removeFile{
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0]stringByAppendingString:@"/file.pdf"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSLog(@"Documents directory before: %@", [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&error]);
if([fileManager fileExistsAtPath:path] == YES)
{
NSLog(@"file exist and I delete it");
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:path error:&error];
NSLog(@"error:%@", error);
}
NSLog(@"Documents directory after: %@", [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&error]);
}
このメソッドは、ディレクトリにNSLogに「file.pdf」があることを認識します
NSLog(@"Documents directory before: %@", [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&error]);
しかし、それは後にクラッシュします
"NSLog(@"file exist and I delete it");"
そして、私はコンソールに「lldb」しか持っていません。