【问题标题】:Issue in saving PDF in my iPad在我的 iPad 中保存 PDF 的问题
【发布时间】:2014-05-27 12:03:49
【问题描述】:

在我的应用程序中,我从网络服务获取我的 PDF 作为 URL。现在我想将它存储在我的 iPad 中并在 adobe Reader App 中打开。我使用以下代码保存它并在 webView 中打开它

NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[
                                                         NSURL URLWithString:socialURLString]];

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

NSString *filePath = [resourceDocPath
                      stringByAppendingPathComponent:@"pdfOne.pdf"];
[pdfData writeToFile:filePath atomically:YES];

// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[web setUserInteractionEnabled:YES];
[web setDelegate:self];
[web loadRequest:requestObj];

现在假设它存储在我的 iPad 中,我打开 Adob​​e Reader 应用程序并没有显示 PDF。我做错了什么?

【问题讨论】:

  • 您确定它已正确存储在文档文件夹中吗?
  • 不。我不知道它是否被存储。
  • @rmaddy:我可以从我的应用程序外部访问 pdf 吗?我的意思是,我想用 Adob​​e Reader 应用程序查看保存的 PDF。这可能吗?

标签: ios ipad pdf


【解决方案1】:

资源包是一个只读目录及其内容文件。所以你应该将文件写入临时目录或某个缓存目录中

////In temporary directory
NSString *tmpFilePath = [NSString stringWithFormat:@"%@/%@.pdf",NSTemporaryDirectory(),@"pdfOne"];

NSString * filePath = tmpFilePath;

///In caches directory.
    NSString *cacheDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *tmpFilePath = [NSString stringWithFormat:@"%@/%@.pdf",cacheDirectoryPath,@"pdfOne"];

    NSString * filePath = tmpFilePath;

【讨论】:

  • 我可以从我的应用程序外部访问 pdf 吗?我的意思是,我想用 Adob​​e Reader 应用程序查看保存的 PDF。这可能吗?
  • 您可以保存在缓存或临时目录中并访问相同的内容。
  • 是的,我已经使用上面的代码将 PDF 保存到缓存。仍然当我打开 Adob​​e Reader 时,保存的 PDF 没有显示在那里.. 我做错了吗?
  • PDF下载到缓存目录了吗?
  • 如何查看是否下载到缓存中?
猜你喜欢
  • 2011-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多