Open pdf stored in Documents folder

Douglas's Avatar

Douglas

15 Jul, 2013 01:17 PM

Hi,

I am trying to open a recently downloaded PDF file and saved into Documents folder.

This is the code I use to open it:

- (void)openRemotePdf:(NSString *)urlForPdf
{
    NSURL *pdfDocument = [NSURL fileURLWithPath:urlForPdf];

   MFDocumentManager *documentManager = [[MFDocumentManager alloc] initWithFileUrl:pdfDocument];
    
    ReaderViewController *pdfViewController = [[ReaderViewController alloc] initWithDocumentManager:documentManager];

    [self presentViewController:pdfViewController animated:YES completion:nil];
    
    pdfViewController = nil;
}

There is something I know for sure: the file was saved and exists in this folder but
MFDocumentManger do not open it. It just shows a background tiled texture and the red logo
that disappears but no document is opened.

what could be the problem?

Thanks

  1. Support Staff 1 Posted by Matteo on 15 Jul, 2013 01:35 PM

    Matteo's Avatar

    Hello,
    could you please paste also the urlForPdf string?

    Best regards,
    Matteo

    On 15/lug/2013, at 15:19, "Douglas" <[email blocked]> wrote:

  2. 2 Posted by Douglas on 15 Jul, 2013 01:42 PM

    Douglas's Avatar

    Sure, here it is:

    Users/angelabennet/Library/Application Support/iPhone Simulator/6.1/Applications/0766420A-A333-4B09-B5F9-4A3F7183D0D1/Documents/A12290-12292.pdf

    Here is where pdf file is saved.

  3. Support Staff 3 Posted by Matteo on 15 Jul, 2013 01:54 PM

    Matteo's Avatar

    It seems that at least a slash as first char is missing. Maybe also a backslash between Application and Support and iPhone Simulator.

    How are you creating that path?

    Something like this should definitely work if the document is present

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"A12290-12292.pdf"];
        [self openRemotePdf:pdfPath];
    

    Let me know.

  4. 4 Posted by Douglas on 15 Jul, 2013 01:58 PM

    Douglas's Avatar

    This is the code I am using, similar to yours:

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask, YES);
        
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:fileToOpen]];
        NSLog(@"path %@", path);
        //UIImage* image = [UIImage imageWithContentsOfFile:path];
        
        //[self listFileAtPath:documentsDirectory];
        [self openRemotePdf:path];

  5. Support Staff 5 Posted by Matteo on 15 Jul, 2013 02:02 PM

    Matteo's Avatar

    Is there any chance that the document is password protected?
    If so you need to unlock it first with
    MFDocumentManager's tryUnlockWithPassword:.

    For an example take a look here

  6. 6 Posted by Douglas on 15 Jul, 2013 03:39 PM

    Douglas's Avatar

    No, it´s a public PDF. No password protection.

    I think that the problem is the same you pointed before. I mean the spaces found in the file path:

    .../Application Support/...

    how can I encode this? By adding '%20'?

  7. Support Staff 7 Posted by Matteo on 15 Jul, 2013 04:33 PM

    Matteo's Avatar

    Hello,
    you can probably add a \ before the space, so:

    .../Application\ Support/iPhone\ Simulator/...

    Matteo

    On 15/lug/2013, at 17:39, Douglas <[email blocked]> wrote:

  8. 8 Posted by Douglas on 16 Jul, 2013 08:33 AM

    Douglas's Avatar

    I added the "\" as you said. I checked it in the console with any other folder and it works (ls Application\ Support) but it does not work in iOS.

  9. 9 Posted by Giuseppe Sollaz... on 23 Jun, 2014 08:26 PM

    Giuseppe Sollazzo's Avatar

    I have a very similar issue. If the pdf is in the bundle, I manage to open it with the standard "three minutes implementation". If I put the pdf in the NSCachesDirectory, it doesn't.

    In other words, this code works:

    NSString *documentName = @"193"; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *thumbnailsPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",documentName]];
    NSURL *documentUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:documentName ofType:@"pdf"]];

    MFDocumentManager *documentManager = [[MFDocumentManager alloc]initWithFileUrl:documentUrl];
    ReaderViewController *pdfViewController = [[ReaderViewController alloc]initWithDocumentManager:documentManager]; documentManager.resourceFolder = thumbnailsPath; pdfViewController.documentId = documentName; [self presentViewController:pdfViewController animated:YES completion:nil];

    But this doesn't - in spite of the variable "filename" containing the right path to the file in the NSCachesDirectory - the same file, put in the mainBundle with the previous code works.

    NSString* bookid = [NSString stringWithFormat:@"193"];
    NSArray paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *thumbnailsPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",bookid]];
    NSString
    cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* filename = [NSString stringWithFormat:@"%@.pdf", bookid];
    NSString *file = [cachesDirectory stringByAppendingPathComponent:filename];
    NSString *cachePath = [paths objectAtIndex:0];
    NSURL *documentUrl = [NSURL fileURLWithPath:[cachePath stringByAppendingPathComponent:file]];
    MFDocumentManager *documentManager = [[MFDocumentManager alloc]initWithFileUrl:documentUrl]; ReaderViewController *pdfViewController = [[ReaderViewController alloc]initWithDocumentManager:documentManager]; documentManager.resourceFolder = thumbnailsPath; pdfViewController.documentId = documentName; [self presentViewController:pdfViewController animated:YES completion:nil];

    Any idea?

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac