Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone I';我无法缓存现有文件的路径_Iphone_Ios_Ipad - Fatal编程技术网

Iphone I';我无法缓存现有文件的路径

Iphone I';我无法缓存现有文件的路径,iphone,ios,ipad,Iphone,Ios,Ipad,这是我的代码,我使用qlviewcontroller来显示一个带有pdf文件的详细视图,我在程序开始时下载并储存了该文件 它抛出一个异常,告诉路径是null -(id<QLPreviewItem>)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)index{ NSString *name=[selectedItem.source last

这是我的代码,我使用
qlviewcontroller
来显示一个带有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!!
    }
-(id)previewController:(QLPreviewController*)previewController PreviewWitematindex:(NSInteger)索引{
NSString*name=[selectedItem.source lastPathComponent];
NSFileManager*fileManager=[NSFileManager defaultManager];
如果([fileManager fileExistsAtPath:name]){
NSLog(@“%@exsit!”,name);//在!!!
}
名称=[name stringByDeletingPathExtension];
NSLog(@“name for QL:%@”,name);//name ok!!!
NSString*path=[[NSBundle mainBundle]pathForResource:类型名称:@“pdf”];
NSLog(@“path:%@”,path);//path:null!!!
返回[NSURL fileURLWithPath:path];//错误!!
}

文件不下载到
[NSBundle mainBundle]
。试着把那行改成

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathString = [documentsDirectory stringByAppendingPathComponent:fileName];` 
而不是从
[NSBundle mainBundle]
读取。您无法将文件下载到mainBundle。它是只读的