这是我的代码,我使用QLPreviewController
来显示详细视图,其中包含我在程序开头下载并存储的pdf文件。
它告诉路径是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!!
}
答案 0 :(得分:0)
文件无法下载到[NSBundle mainBundle]
。尝试将该行更改为
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathString = [documentsDirectory stringByAppendingPathComponent:fileName];`
而不是从[NSBundle mainBundle]
读取。您无法将文件下载到mainBundle。这是只读的。