2016-07-21 73 views
0

我正在从JSON保存PDF。当我浏览到在App文件目录的文件路径,以验证我有正确的文件,并尝试打开PDF,我无法打开PDF,我得到这个错误:将PDF保存为文档(PDF已损坏)

enter image description here

enter image description here

文件路径是好的,该文件是存在的,但它只是被损坏或东西。我认为这与NSData有关,但我不确定我做错了什么。有什么想法吗?谢谢!

的ViewController:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    NSLog(@"Downloading PDF Started"); 

    // Get selected PDF 
    NSString *pdfSelected = self.pdfArray[indexPath.row]; 
    NSLog(@"PDFSl: %@", pdfSelected); 
    NSData *pdfData = [NSData dataWithContentsOfURL:[NSURL URLWithString:pdfSelected]]; 
    NSLog(@"PDFData: %@", pdfData); 
    NSString *pdfPath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%ld.pdf",@"savedPDF", (long)indexPath.row]]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     [pdfData writeToFile:pdfPath atomically:YES]; 
     NSLog(@"PDF File Saved!"); 
     NSLog(@"PDF File Path: %@", pdfPath); 
     // Save PDF path in defaults for evaluation in Details 
     [defaults setObject:pdfPath forKey:@"cachedPDFPath"]; 
    }); 

}); 

JSON片段:

{ 
     "fname": "Nadene", 
     "lname": "Feehan", 
     "email": "[email protected]", 
     "phone": "(152) 555-5321", 
     "image": "http://logok.org/wp-content/uploads/2014/04/Apple-logo-grey-880x625.png", 
     "video": "https://github.com/versluis/Movie-Player/blob/master/Movie%20Player/video.mov?raw=true", 
     "pdf": "http://adcdownload.apple.com/Developer_Tools/Xcode_8_beta_3/Release_Notes_for_Xcode_8_beta_3.pdf" 
    }, 

控制台:

PDFSl:http://adcdownload.apple.com/Developer_Tools/Xcode_8_beta_3/Release_Notes_for_Xcode_8_beta_3.pdf

PDFDataLength: 2070

PDF File Path: /Users/user/Library/Developer/CoreSimulator/Devices/1/data/Containers/Data/Application/A/Documents/savedPDF0.pdf

我也试过:

NSData *pdfData = [NSData dataWithContentsOfURL:[NSURL URLWithString:pdfSelected] options:NSDataReadingUncached error:&errorPDF];但是当我检查控制台看到的错误是,如果你不与你的开发者帐户登录,没有PDFDataError: (null)

+0

你能显示控制台日志(具体地说,记录语句的结果,你显示)的内容是什么?另外,当你记录'PDFData'时,你还可以记录数据的长度(即'pdfData.length')吗?此外,如果没有显示任何有趣的内容,你可以尝试使用'dataWithContentsOfURL:options:error:'来代替更简单的'dataWithContentsOfURL:'来查看你是否真的并且成功地下载了数据。 – fullofsquirrels

+0

@fullofsquirrels没问题,只是添加了控制台日志。你有什么特别的吗? – SRMR

+1

如果您未使用开发者帐户登录,网址就会被锁定;当我试图瓦特/出在第一已签署,我重定向到这个网页:https://developer.apple.com/unauthorized/ 你可能需要理清如何处理谁不签约用户到他们的开发者账户中(并且为了使事情更加复杂,我相信,在20分钟或一小时不活动之后,developer.apple.com会自动签名),并且它看起来不像简单的检查那样简单HTTP状态/错误代码。 – fullofsquirrels

回答

1

貌似网址被锁定;当我尝试使用首次登录时,我将重定向到此页面:developer.apple.com/unauthorized。正如你所期望的那样,内容是纯粹的HTML,而且我猜测许多(如果不是绝大多数的话)PDF解析器会在发送HTML时崩溃。

你可能需要理清如何处理谁不签入自己的开发者帐户(用户,使事情变得更加复杂,我相信developer.apple.com自动迹象,你出像20后几分钟或一小时不活动),并且它看起来不像简单地检查HTTP状态/错误代码那样简单。