iphone pdf download

2019-01-26 02:05发布

        -(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]; 
        } 

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 ?

2条回答
何必那么认真
2楼-- · 2019-01-26 02:51

check this out, I made it for image but you can easily mod it to do PDF. I hope it helps.

查看更多
Viruses.
3楼-- · 2019-01-26 03:02

To check if the file exists, use:

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

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'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];

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

查看更多
登录 后发表回答