存储PDF本地不工作在iphone

问题描述:

我是新的iPhone开发。我想使用url下载pdf并想将其存储在本地文件夹中。存储PDF本地不工作在iphone

这里是我的代码:

NSData *d = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://demo.com/ebookapplication/book_pdf/20111108094614hii.pdf"]]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myfile.pdf"]; 
[d writeToFile:path atomically:YES]; 
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) 
{ 
NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:@"myfile" ofType:@"pdf"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    [fileManager copyItemAtPath:myPathInfo toPath:path error:NULL]; 
} 
NSURL *pdfurl = [NSURL fileURLWithPath:path]; 
NSURLRequest *requestObj = [NSURLRequest requestWithURL:pdfurl]; 
[detail.bookweb loadRequest:requestObj];//to display data in web view 

该代码使用URL在Web视图工作显示PDF格式,但它并没有在我们的本地系统中存储的PDF工作。其实我想将这些下载的pdf存储在一个文件夹中,并以库的格式显示。请帮忙。

+1

“你在本地系统中存储”是什么意思?您已经将其本地存储到文档文件夹中。 – Ilanchezhian 2011-12-21 11:41:35

+0

在本地系统中存储意味着我想将这个文件存储在我的项目文件夹中,从那里我将能够读取该文件。我已经在文档文件夹中搜索此文件,但我没有得到这个文件。我使用xcode 4.2 – spatil 2011-12-22 04:39:49

-writeToFile:原子已经将文件放置在-copyItemAtPath:toPath:方法中指定的文件夹中,因此是多余的。该文档已经在文档目录文件夹中,然后将其复制到相同的目录中!我认为这是你的问题。

编辑:

此线的位置:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myfile.pdf"]; 

指定的路径。

您可以使用此行NSLog它:NSLog(@"Document Path = %@", path);

至于你的库评论,它不应该是那么难。我的应用程序,这基本上已经做了这个,电线 AQGridView:https://github.com/AlanQuatermain/AQGridView

和苹果的Docwatcher类从DITableView:http://developer.apple.com/library/ios/samplecode/DocInteraction/Listings/Classes_DITableViewController_h.html

共同打造的是巨魔的文件目录,将文件添加到一个NSArray然后将其显示的应用程序由电网。一个更简单的方法就是使用DITableViewController,但考虑到它解决了你的问题,它只是为了更多的比萨饼。

+0

感谢CodaFi,我可以使用Docwatcher实现library.but你可以告诉我如何解决我的文件复制问题?我想将所有pdf文件存储在一个文件夹中。我可以设置路径到该文件夹​​吗? – spatil 2011-12-22 04:58:05

+0

您的writeToFile方法已经设置了文件路径。 – CodaFi 2011-12-22 17:42:56

+0

但这个路径被设置?我的意思是文件夹名?如果它是文档文件夹我搜索文件夹中的文件,但文件不存在..plz帮助 – spatil 2011-12-23 06:24:47