在我的iPad中保存PDF的问题

时间:2014-05-27 12:03:50

标签: ios ipad pdf

在我的应用中,我将PDF作为来自网络服务的网址。现在我想将它存储在我的i​​Pad中并在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];

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

1 个答案:

答案 0 :(得分:0)

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

////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;