当试图在新视图中打开下载的pdf时崩溃

问题描述:

我的应用下载PDF然后在按钮按下时将其显示在新视图中。当试图在新视图中打开下载的pdf时崩溃

我得到的错误:

-[NSURL initFileURLWithPath:]: nil string parameter' 

一些故障排除我压住了问题的地方在此代码段后。指向的路径位于下载的pdf所在的/Documents文件夹中。因此该文件不在主包中。

NSString *path = [[NSBundle mainBundle] pathForResource:PDFpathwithextension ofType:@"pdf"]; 
NSURL *url = [NSURL fileURLWithPath:path]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

这里的下载代码:

//Start an NSURL connection to download from the remotepath 
    NSData *pdfData = [[NSData alloc] initWithContentsOfURL:remotepathURL]; 

//Store the Data locally as PDF File 
    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]]; 

    NSString *filePath = [resourceDocPath stringByAppendingPathComponent:[newdata.ThirdPickerName stringByAppendingFormat:@".pdf"]]; 
    pdfData writeToFile:filePath atomically:YES]; 
+0

从技术上讲,你在这里看到一个异常,这导致崩溃 – 2013-04-09 15:54:32

由于NSURL告诉你,你已经交给它的nil,而不是一个有效的路径。

nil这里表示没有这样的资源可以找到这个名字。事实上,你的问题表明你很清楚这一点。

既然你声称你的应用程序已经下载了一个PDF文件,它是否真的把它写入磁盘?如果是这样,你应该知道其中由此产生的文件。如果没有,你首先需要写下实际的下载代码!

+0

添加了下载代码,任何看起来可疑? – Bogiematch 2013-04-09 17:06:17

+0

你应该直接使用'NSURLConnection',以避免阻塞任何线程(尤其是主线程)。同时避免一次将所有文件放在内存中 – 2013-04-09 17:10:35

+0

您的代码决定放置文件的位置非常可怕。使用' - [NSFileManager URLForDirectory:inDomain:appropriateForURL:create:error:]' – 2013-04-09 17:14:56