iphone pdf下载 [英] iphone pdf download

查看:118
本文介绍了iphone pdf下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        -(IBAction)click;
    {

            NSURL *url = [NSURL URLWithString:URL_TO_DOWNLOAD]; 
            NSString *tempDownloadPath = [[self documentsDirectory] 
                                          stringByAppendingPathComponent:@"test.pdf"]; 
            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
            [request setDownloadDestinationPath:[self documentsDirectory]]; 
            [request setTemporaryFileDownloadPath:tempDownloadPath]; 
            [request setDelegate:self]; 

  [request startAsynchronous]; 


        } 

    - (void)requestFinished:(ASIHTTPRequest *)request 
        { 
            NSLog(@"requestFinished"); 
            NSError *error; 
            NSFileManager *fileManager = [[NSFileManager alloc] init]; 
            NSLog(@"Documents directory: %@", [fileManager 
                                               contentsOfDirectoryAtPath:[self 
                                                                          documentsDirectory] error:&error]); 
            NSString *dir = [self documentsDirectory]; 
            NSLog(dir); 
            // NSData *responseData = [request responseData]; 
            NSArray *array = [[NSFileManager defaultManager] 
                              contentsOfDirectoryAtPath:dir error:&error]; 
            if (array == nil) { 
                NSLog(@"array == nil"); 
            } 
else
{
[aWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:tempDownloadPath]]];
        [[self view] addSubview:aWebView];  
}

            [fileManager release]; 
                } 

    - (NSString *)documentsDirectory { 
            NSArray *paths = 
            NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                NSUserDomainMask, 
                                                YES); 
            return [paths objectAtIndex:0]; 
        } 

我无法通过点击再次下载之前检查文件是否存在,或实际显示请求已完成的pdf。任何解决方案?

I cant manage to check if the file exists before downloading again with an click, or actually displaying the pdf one the request has finished. Any solutions to this ?

推荐答案

要检查文件是否存在,请使用:

To check if the file exists, use:

BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:file]

要在请求完成后显示PDF,您可以在 UIWebView 中打开它,或使用 CGPDF * 用于呈现它的函数集。

To display the PDF once the request has finished, you can either open it in a UIWebView or use the CGPDF* set of functions to render it.

ASIHTTPRequest的 setDownloadDestinationPath 期望接收绝对文件路径,它看来你只是传递文件目录。
您可以从您的URL获取文件名并将其附加到文档目录路径:

ASIHTTPRequest's setDownloadDestinationPath expects to receive an absolute file path, and it seems you're just passing the documents directory instead. You could get the filename from your URL and append it to the documents directory path:

NSString *filename = [[url absoluteString] lastPathComponent];

NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
NSString *destPath = [directory stringByAppendingPathComponent:filename];

[request setDownloadDestinationPath:destPath];

然后检查下载的文件是否确实存在,你可以使用 destPath 再次。

Then to check if the downloaded file actually exists, you can use destPath again.

这篇关于iphone pdf下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆