I'm having trouble cacheing the path of an existing file
Here is my code, I was using a QLPreviewController
for display a detail view with a pdf file which I download and stock at the beginning of the program.
It throw a exception with telling the path was null
.
-(id<QLPreviewItem>)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)index{
NSString *name=[selectedItem.source lastPathComponent];
NSFileManager *fileManager=[NSFileManager defaultManager];
if([fileManager fileExistsAtPath:name] ){
NSLog(@"%@ exsit! ",name); //In!!!
}
name = [name stringByDeletingPathExtension];
NSLog(@"name for QL: %@",name); //Name ok!!!
NSString *path = [[NSBundle mainBundle]pathForResource:name ofType:@"pdf"];
NSLog(@"path: %@",path); //Path: null !!!
return [NSURL fileURLWithPath:path]; //error!!
}
开发者_Python百科
Files don't download to [NSBundle mainBundle]
. Try changing that line to
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathString = [documentsDirectory stringByAppendingPathComponent:fileName];`
rather than reading from [NSBundle mainBundle]
. You can't download files to mainBundle. It's read only.
精彩评论