将PDF保存到文档(PDF已损坏)

时间:2016-07-21 19:08:23

标签: ios objective-c json pdf

我从JSON保存PDF。当我导航到App Documents Directory中的文件路径以验证我有正确的文件,并尝试打开PDF时,我无法打开PDF ,我收到此错误:

enter image description here

enter image description here

文件路径很好,文档在那里,但它刚刚被破坏或者其他东西。我认为这与cffi有关,但我不确定我做错了什么。有什么想法吗?谢谢!

的ViewController:

NSData

JSON片段:

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"];
    });

});

控制台:

{ "fname": "Nadene", "lname": "Feehan", "email": "nadene.feehan@gmail.com", "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];

1 个答案:

答案 0 :(得分:1)

如果您未使用开发者帐户登录,则会看到该网址已被锁定;当我第一次尝试登录时,我被重定向到这个页面:developer.apple.com/unauthorized。正如您所料,内容是纯HTML,如果您发送HTML,我会猜测许多(如果不是大多数)PDF解析器会崩溃。

您可能需要理清如何处理未登录其开发者帐户的用户(并且为了使事情变得更复杂,我相信developer.apple.com会在以后自动注销您类似20分钟或一小时不活动的东西,并不像简单地检查HTTP状态/错误代码一样简单。