在本地保存NSURL的PDF - 检索

时间:2014-04-11 07:36:00

标签: ios pdf uiwebview nsdictionary nsurl

您好,

如何从NSUrl本地检索PDF NSDictionaryNSData *pdfData = [[NSData alloc] initWithContentsOfURL:[ NSURL URLWithString:@"http://www.example.com/info.pdf"]]; // Store the Data locally as PDF File NSString *resourceDocPath = [[NSString alloc] initWithString:[ [[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents" ]]; NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"myPDF.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]; [webView setDelegate:self]; [webView loadRequest:requestObj]; 。我这样保存:

UIWebView

但是我如何检索文件并将其显示在{{1}} ??

干杯, SebOH

2 个答案:

答案 0 :(得分:2)

试试这个解决方案。下载文件后,您不必每次都下载。您可以访问存储文件的目录。

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


        NSString *filePath = [resourceDocPath
                              stringByAppendingPathComponent:@"myPDF.pdf"];
        if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[
                                                                 NSURL URLWithString:@"http://www.example.com/info.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];
        [webView setDelegate:self];
        [webView loadRequest:requestObj];

答案 1 :(得分:1)

你正在做的事情,“检索文件并在UIWebView中显示它?”使用以下代码

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

[webView setDelegate:self];
[webView loadRequest:requestObj];

您是否面临上述代码行的问题,是否在webview中加载了PDF?

因此,如果您不想每次都执行此检查时下载文件,

if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
//download the file
}

所以整个解决方案都是

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

NSString *filePath = [resourceDocPath 
    stringByAppendingPathComponent:@"myPDF.pdf"];

if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        //download the file
        NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[
        NSURL URLWithString:@"http://www.example.com/info.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];

[webView setDelegate:self];
[webView loadRequest:requestObj];